【发布时间】:2019-01-08 01:40:13
【问题描述】:
我的数据库对象都继承自具有一些基本属性的父类。
基础实体
public abstract class Entity : IAuditable
{
public int Id { get; set; }
public string CreatedBy { get; set; }
public int? CreatedDate { get; set; }
public string UpdateBy { get; set; }
public int? UpdatedDate { get; set; }
}
基础实体配置文件
public EntityConfiguration()
{
HasKey(e => e.Id);
Property(e => e.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(e => e.CreatedBy)
.IsUnicode(false)
.HasMaxLength(255);
Property(e => e.UpdatedBy)
.IsUnicode(false)
.HasMaxLength(255);
}
我在所有子类上子类化实体以继承公共属性
public class User : Entity
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
DbContext
public class SomeContext : DbContext
{
public SomeContext() : base("name=DefaultConnection")
{
}
public static SomeContext Create()
{
return new SomeContext();
}
public DbSet<Collaboration> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new EntityConfiguration());
modelBuilder.Configurations.Add(new UserConfiguration());
}
}
当我创建迁移时,它将正确创建所有继承的属性,但即使我没有 DbSet,它仍会创建父类(实体)。如果我删除在 OnModelCreating 方法中添加配置设置的行,我不会获得最大长度和 Unicode 设置。
有没有一种方法可以在不实际创建父表且不将配置添加到所有子类配置文件的情况下使用配置属性。
【问题讨论】:
标签: c# sql-server entity-framework-6