我认为这在nhiberate中是可能的,但我的问题是关于实体框架.
在我的数据库模型中 – 我无法修改 – 我有冗余列,我想存储在不同的类中.@H_403_3@
示例:@H_403_3@
public class DateParams { public DateTime CreationDate { get; set; } public DateTime ModificationDate { get; set; } // some methods } public class Localization { public String EnglishLabel { get; set; } public String FrenchLabel { get; set; } // some methods }@H_403_7@然后我会在我的一些模型中使用它们:@H_403_3@
public class Account // Localization && DateParams { public int ID { get; set; } public String Name { get; set; } public Localization Localization { get; set; } public DateParams DateParams { get; set; } } public class Lead // DateParams only { public int ID { get; set; } public String Name { get; set; } public DateParams DateParams { get; set; } }@H_403_7@我想要实现的是拥有这样的东西@H_403_3@
public class LocalizationMap : EntityTypeConfiguration<Localization> { public LocalizationMap() { Property(e => e.EnglishLabel).HasColumnName("en"); Property(e => e.FrenchLabel).HasColumnName("fr"); } } public class AccountMap : EntityTypeConfiguration<Account> { public AccountMap() { HasKey(x => x.ID); Property(e => e.Name).HasColumnName("Name"); HasSubMapping(new LocalizationMap()); HasSubMapping(new DateParamsMap()); ToTable("Account"); } }@H_403_7@
解决方法
您可以通过使用复杂类型来实现此目的.这些映射到名为complextypeName_propertyName的表列,但可以通过在DbContext中覆盖OnModelCreating(DbModelBuilder modelBuilder)来更改此行为,如
Entity Framework – Reuse Complex Type中所述
对于你的例子:@H_403_3@
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.ComplexType<Localization>(); modelBuilder.Entity<Account>().Property(x => x.Localization.EnglishLabel).HasColumnName("en"); modelBuilder.Entity<Account>().Property(x => x.Localization.FrenchLabel).HasColumnName("fr"); // et cetera }@H_403_7@