代码之家  ›  专栏  ›  技术社区  ›  Catalin

EF Code First防止使用Fluent API进行属性映射

  •  32
  • Catalin  · 技术社区  · 13 年前

    我有课 Product 和一个复杂的类型 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
    }
    

    是否可以阻止映射“国家/地区”属性 地址详细信息 在…内 产品 班(因为我永远都不需要它 产品 类别)

    像这样的

    Property(p => p.AddressDetails.Country).Ignore();
    
    7 回复  |  直到 13 年前
        1
  •  30
  •   Community Mohan Dere    8 年前

    对于EF5及以上版本: DbContext.OnModelCreating 覆盖您的上下文:

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

    对于EF6: 你运气不好。看见 Mrchief's answer .

        2
  •  15
  •   Mrchief    11 年前

    不幸的是,接受的答案不起作用,至少在EF6中是不起作用的,尤其是如果子类不是实体的话。

    我还没有找到通过流畅的API实现这一点的方法。它的唯一工作方式是通过数据注释:

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

    注: 如果你有这样的情况 Country 只有当它是某个其他实体的一部分时,才应该被排除在外,那么你就不适合这种方法了。

        3
  •  10
  •   JAVizcaino    11 年前

    如果您正在使用EntityTypeConfiguration的实现,则可以使用Ignore方法:

    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");
    }
    
        4
  •  4
  •   Salizar Marxx    9 年前

    虽然我意识到这是一个老问题,但答案并没有解决我对EF 6的问题。

    对于EF 6,您需要创建一个ComplexTypeConfiguration映射。

    例子:

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

    如果您的Context正在手动加载配置,则需要添加新的ComplexMap,如果您使用FromAssembly重载,则它将与其他配置对象一起使用。

        5
  •  2
  •   davidfcruz    10 年前

    在EF6上,您可以配置复杂类型:

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

    这样一来,Country属性将始终被忽略。

        6
  •  1
  •   Wai Ha Lee captain-yossarian from Ukraine    10 年前

    试试这个

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

    在类似的情况下,它对我有效。

        7
  •  -2
  •   Tommy Ceusters    11 年前

    也可以在Fluent API中完成,只需在映射中添加以下代码

    this.Ignore(t=>t.Country),在EF6中测试