允许这个人问别人帮助这个人。
这是将我的数据库应用程序从SQLalchemy迁移到EF Core。
因为EF Core支持每种类型的表(联合表继承)。
我在下面显示我的代码
班级
public class Degreaser_Old
{
public Degreaser_Old()
{ }
public Degreaser_Old(string tubname)
{
ChemistryContext Context = ContextMaker.MainContext();
DegreaserPosNavigation = Context.DegreaserPos
.Include(s => s.Degreaser)
.SingleOrDefault(s => s.EngName == tubname);
}
public int DegreaserCode { get; set; }
public int SarogateKey { get; set; }
public int DegreaserPos { get; set; }
public double DegreaserConc { get; set; }
public string Type { get; set; }
public DegreaserPos DegreaserPosNavigation { get; set; }
public virtual MeasureDay SarogateKeyNavigation { get; set; }
}
public class Degreaser : Degreaser_Old
{
public Degreaser()
{ }
public Degreaser(string tubname) : base(tubname)
{ }
public decimal Level { get; protected set; }
public decimal Volume { get; protected set; }
}
上下文配置(流利的API)
modelBuilder.Entity<Degreaser_Old>(entity =>
{
entity.ToTable("Degreaser");
entity.HasKey(e => e.DegreaserCode);
entity.HasAlternateKey(a => new { a.SarogateKey, a.DegreaserPos });
entity.HasIndex(e => new { e.SarogateKey, e.DegreaserPos })
.IsUnique(true);
entity.HasDiscriminator(c => c.Type)
.HasValue<Degreaser_Old>("Old")
.HasValue<Degreaser>("Degreaser");
entity.HasIndex(e => e.SarogateKey)
.HasDatabaseName("Degreaser$測定日コード");
entity.Property(e => e.DegreaserCode).ValueGeneratedOnAdd();
entity.Property(e => e.DegreaserConc).HasPrecision(5, 2);
entity.Property(e => e.DegreaserPos).HasColumnName("Degreaser_Pos");
entity.Property(e => e.DegreaserCode).ValueGeneratedOnAdd();
entity.Property(e => e.Type);
entity.HasOne(d => d.DegreaserPosNavigation)
.WithMany(p => p.Degreaser)
.HasForeignKey(d => d.DegreaserPos)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("Degreaser${8BC67109-2594-4BBD-848F-EB4D77039677}");
entity.HasOne(d => d.SarogateKeyNavigation)
.WithMany(p => p.Degreasers)
.HasForeignKey(d => d.SarogateKey)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("Degreaser${5C8E5635-10BD-4124-8D13-FCC511573DC5}");
});
modelBuilder.Entity<Degreaser>(entity =>
{
entity.ToTable("Degreaser_Vol");
entity.HasBaseType<Degreaser_Old>();
entity.Property(c => c.Level).HasMaxLength(5);
entity.Property(c => c.Volume).HasPrecision(4,2);
});
运行添加迁移时的消息
“ Degreaser”映射到表“ Degreaser_Vol”,而“ Degreaser_Old”映射到表“ Degreaser”。将层次结构中的所有实体类型映射到同一张表或删除区分符,并将它们全部映射到不同的表。有关更多信息,请参见https://go.microsoft.com/fwlink/?linkid=2130430。
这个可以理解的说法是必须使用TPH或TPC。
但是这个人没有足够的知识来解决它。
这个问题困扰着我。