EF Code First prevent property mapping with Fluent API

25,349

Solution 1

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.

Solution 2

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:

public class AddressDetails
{
    public string City { get; set; }

    [NotMapped]
    public string Country { get; set; }
    // other properties
}

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.

Solution 3

If you are using an implementation of EntityTypeConfiguration you can use the Ignore Method:

public class SubscriptionMap: EntityTypeConfiguration<Subscription>
{
    // Primary Key
    HasKey(p => p.Id)

    Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    Property(p => p.SubscriptionNumber).IsOptional().HasMaxLength(20);
    ...
    ...

    Ignore(p => p.SubscriberSignature);

    ToTable("Subscriptions");
}

Solution 4

While I realize that this is an old question, the answers didn't resolve my issue with EF 6.

For EF 6 you need to create a ComplexTypeConfiguration Mapping.

example:

public class Workload
{
    public int Id { get; set; }
    public int ContractId { get; set; }
    public WorkloadStatus Status {get; set; }
    public Configruation Configuration { get; set; }
}
public class Configuration
{
    public int Timeout { get; set; }
    public bool SaveResults { get; set; }
    public int UnmappedProperty { get; set; }
}

public class WorkloadMap : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Workload>
{
    public WorkloadMap()
    {
         ToTable("Workload");
         HasKey(x => x.Id);
    }
}
// Here This is where we mange the Configuration
public class ConfigurationMap : ComplexTypeConfiguration<Configuration>
{
    ConfigurationMap()
    {
       Property(x => x.TimeOut).HasColumnName("TimeOut");
       Ignore(x => x.UnmappedProperty);
    }
}

If your Context is loading configurations manually you need to add the new ComplexMap, if your using the FromAssembly overload it'll be picked up with the rest of the configuration objects.

Solution 5

On EF6 you can configure the complex type:

 modelBuilder.Types<AddressDetails>()
     .Configure(c => c.Ignore(p => p.Country))

That way the property Country will be always ignored.

Share:
25,349
Catalin
Author by

Catalin

Hello, My name is Catalin and I am a full-stack developer with the primary focus on .NET. Some of the technologies I like to work with include: ASP.NET Core, AWS, Azure, MongoDB, JavaScript, RequireJS, Gulp, React, RabbitMQ, AWS SAM I like challenges, I am a strong advocate of constant refactoring and I try to avoid coding compromises as much as possible. On my spare time I like to work on my personal project, https://github.com/KissLog-net/KissLog.Sdk Some of my favorite (professional) books are: Adaptive Code via C#: Agile coding with design patterns and SOLID principles Learning JavaScript Design Patterns Pragmatic Programmer

Updated on May 26, 2020

Comments

  • Catalin
    Catalin almost 4 years

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