EF6 - TPT - 继承 - 数据库优先
EF6 - TPT - Inheritance - Database First
我有 2 个表,Products(我的基础)和 OffShelfItems(来自 child)
我在 ID 字段上设置了外键,这是此设置的屏幕截图:
在我的 EDMX 中,我导入了表格并将 OffShelfItem 设置为产品的成员 class,这是该设置的屏幕截图:
但是,当我尝试使用此测试代码保存 object 时:
OffShelfItem osi = new OffShelfItem();
osi.WhenAdded = DateTime.Now;
osi.LastModified = DateTime.Now;
osi.IsDeleted = false;
osi.Title = "TEST ITEM";
osi.RetailPrice = 9.99M;
osi.DealerPrice = 7.99M;
ent.Products.Add(osi);
ent.SaveChanges();
我收到这个错误:
Schema specified is not valid. Errors: App_Code.Model.ssdl(75,6) :
error 0113: Multiplicity is not valid in Role 'AA_OffShelfItems' in
relationship 'FK_AA_OffShelfItems_AA_Products'. Because the Dependent
Role refers to the key properties, the upper bound of the multiplicity
of the Dependent Role must be 1.
我觉得我一定非常接近让这个工作,我只是需要帮助才能做到这一点!
id
需要成为 AA_OffShelfItems
table 上的主键。
这里解释得很好:
[更新:]
然后您可以按照此答案中所述扩展您的上下文来访问 OffShelfItems
:
EF Database First with TPT Inheritance only creates DbSet<T> for base clases
类似:
context.Products.OfType<OffShelfItem>()
或:
partial class Context
{
public DbSet<OffShelfItem> OffShelfItem{ get; set; }
}
我有 2 个表,Products(我的基础)和 OffShelfItems(来自 child)
我在 ID 字段上设置了外键,这是此设置的屏幕截图:
在我的 EDMX 中,我导入了表格并将 OffShelfItem 设置为产品的成员 class,这是该设置的屏幕截图:
但是,当我尝试使用此测试代码保存 object 时:
OffShelfItem osi = new OffShelfItem();
osi.WhenAdded = DateTime.Now;
osi.LastModified = DateTime.Now;
osi.IsDeleted = false;
osi.Title = "TEST ITEM";
osi.RetailPrice = 9.99M;
osi.DealerPrice = 7.99M;
ent.Products.Add(osi);
ent.SaveChanges();
我收到这个错误:
Schema specified is not valid. Errors: App_Code.Model.ssdl(75,6) : error 0113: Multiplicity is not valid in Role 'AA_OffShelfItems' in relationship 'FK_AA_OffShelfItems_AA_Products'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be 1.
我觉得我一定非常接近让这个工作,我只是需要帮助才能做到这一点!
id
需要成为 AA_OffShelfItems
table 上的主键。
这里解释得很好:
[更新:]
然后您可以按照此答案中所述扩展您的上下文来访问 OffShelfItems
:
EF Database First with TPT Inheritance only creates DbSet<T> for base clases
类似:
context.Products.OfType<OffShelfItem>()
或:
partial class Context
{
public DbSet<OffShelfItem> OffShelfItem{ get; set; }
}