Search code examples
c#entity-frameworkef-code-firstfluent-interface

EF Code First prevent property mapping with Fluent API


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();

Solution

  • For EF5 and older: In the DbContext.OnModelCreating override for your context:

    modelBuilder.Entity<Product>().Ignore(p => p.AddressDetails.Country);
    

    For EF6: You're out of luck. See Mrchief's answer.