I have a class Product
and a complex type AddressDetails
public class Product
{
public Guid Id { get; set; }
public AddressDetails AddressDetails { get; set; }
}
public class AddressDetails
{
public string City { get; set; }
public string Country { get; set; }
// other properties
}
Is it possible to prevent mapping "Country" property from AddressDetails
inside Product
class? (because i will never need it for Product
class)
Something like this
Property(p => p.AddressDetails.Country).Ignore();
If you are using an implementation of EntityTypeConfiguration you can use the Ignore Method:
It can be done in Fluent API as well, just add in the mapping the following code
this.Ignore(t => t.Country), tested in EF6
Try this
It worked for me in similar case.
For EF5 and older: In the
DbContext.OnModelCreating
override for your context:For EF6: You're out of luck. See Mrchief's answer.
Unfortunately the accepted answer doesn't work, not at least with EF6 and especially if the child class is not an entity.
I haven't found any way to do this via fluent API. The only way it works is via data annotations:
Note: If you have a situation where
Country
should be excluded only when it is part of certain other entity, then you're out of luck with this approach.On EF6 you can configure the complex type:
That way the property Country will be always ignored.