我正在尝试为具有某种固有性的小型代码库创建初始迁移(我尝试从较大的代码库中提取问题;)) 有几个抽象类级别(带有和不带有泛型),但是EntityFramework似乎无法发现类型。
如果我删除通用(我不想做的事情),则会生成迁移,并且看起来可以。
我们正在使用“每类型表格”继承策略。
DbContext:
public DbSet<NodeBase> PipeNodeBases { get; set; }
public DbSet<NodeLevelOneBase> PipeNodeLevelOneBases { get; set; }
public DbSet<Node> PipeNodeCoils { get; set; }
/// <inheritdoc />
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
SetUpDateTime(modelBuilder);
SetUpConventions(modelBuilder);
SetUpEntityConfigurations(modelBuilder);
base.OnModelCreating(modelBuilder);
}
private void SetUpEntityConfigurations(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<NodeBase>()
.ToTable("NodesBase")
.Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<NodeLevelOneBase>()
.ToTable("NodesLevelOne");
modelBuilder.Entity<Node>()
.ToTable("Nodes");
modelBuilder.Entity<SomethingBase>()
.ToTable("SomethingBases")
.Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Something>()
.ToTable("Somethings");
}
protected virtual void SetUpConventions(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add<TablePerTypeConvention>();
modelBuilder.Conventions.Add<ManyToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Add<OneToManyCascadeDeleteConvention>();
}
protected virtual void SetUpDateTime(DbModelBuilder modelBuilder)
{
modelBuilder.Properties<DateTime>().Configure(c => c.HasColumnType("datetime2"));
}
模型:
public abstract class SomethingBase
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
}
public class Something : SomethingBase { }
public abstract class NodeBase
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
}
public abstract class NodeLevelOneBase : NodeBase
{
public string T { get; set; }
}
public abstract class NodeLevelTwoBase<TMaterial> : NodeLevelOneBase
where TMaterial : SomethingBase
{
public virtual TMaterial Something { get; set; }
}
public class Node : NodeLevelTwoBase<Something>
{
}
我收到的错误是:
抽象类型'Domain.Model.NodeLevelOneBase'没有映射 后代,因此无法映射。要么删除 模型中的“ Domain.Model.NodeLevelOneBase”或添加一个或多个 从“ Domain.Model.NodeLevelOneBase”派生到模型的类型。
我希望EntityFramework通过Node找到NodeLevelTwoBase作为映射后代。