EF Code First prevent property mapping with Fluent API

后端 未结 7 1195
鱼传尺愫
鱼传尺愫 2020-12-29 20:42

I have a class Product and a complex type AddressDetails

public class Product
{
    public Guid Id { get; set; }

    public Addres         


        
相关标签:
7条回答
  • 2020-12-29 21:21

    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.

    0 讨论(0)
提交回复
热议问题