C# EF核心迁移与抽象类的OwnsMany
我有一些使用EF Core迁移的经验,并且成功地使用了C# EF核心迁移与抽象类的OwnsMany,c#,entity-framework-core,C#,Entity Framework Core,我有一些使用EF Core迁移的经验,并且成功地使用了.OwnsMany()。 在这种情况下,我无法为我的模型获得工作迁移 我所拥有的是: public class Class1 { public string Id { get; set; } public List<OwnedBaseClass> SomeList { get; set; } } public abstract class OwnedBaseClass { public string Ba
.OwnsMany()
。
在这种情况下,我无法为我的模型获得工作迁移
我所拥有的是:
public class Class1
{
public string Id { get; set; }
public List<OwnedBaseClass> SomeList { get; set; }
}
public abstract class OwnedBaseClass
{
public string BaseProperty { get; set; }
}
public class AA : OwnedBaseClass
{
public string AAProperty { get; set; }
}
public class BB : OwnedBaseClass
{
public string BBProperty { get; set; }
}
添加迁移时,出现以下错误:
The corresponding CLR type for entity type 'OwnedBaseClass' cannot be instantiated, and there is no derived entity type in the model that corresponds to a concrete CLR type.
我好像绕不开这件事。
如果我为AA类型添加一个DbSet(这似乎是错误的),我会得到一个错误:
The entity type 'AA' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating
如果我再加上(更糟)
这就是我被困的地方。任何帮助都将不胜感激
感谢阅读。这似乎是您理解关系数据库时的一个错误。派生类型需要在两个表之间设置关系(主键到外键)。声明不需要键,就是说派生类型是派生类型,而不指定哪个父类是它的基类。迁移中似乎不存在“OwnedBaseClass”,这意味着系统试图在基表存在之前实现派生类型。这显然行不通,原因与上述相同。@MortenBork-我不确定我是否理解你的意思。从我的第一个示例中,我知道当我为AA和BB声明DbSet时,我可以为OwnedBaseClass创建一个带有鉴别器列的表来区分驱动类型。我需要的是将这个表创建为Class1所有。
The corresponding CLR type for entity type 'OwnedBaseClass' cannot be instantiated, and there is no derived entity type in the model that corresponds to a concrete CLR type.
The entity type 'AA' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating
modelBuilder.Entity<AA>().HasNoKey();
'AA' cannot be configured as keyless because it is a derived type; the root type 'OwnedBaseClass' must be configured as keyless instead.