EF:当抽象基和一些具体类型位于 TPH 表中并且其他类型有自己的表时,我可以混合 TPH 和 TPT 吗?
首先,这些问题相似但绝对不一样:
我可以在实体框架中混合按层次结构表和按类型表吗? - 指不同的场景。
实体框架:按类型混合表和按层次结构表< /a> - 另一种情况,尽管接受第一种情况的答案,但与之无关 (*)。
(*) 其次,当使用每个实体表和带有每个层次结构表的鉴别器的表映射基本实体时,我在实体框架中成功混合了每个类型表和每个层次结构表位于链条的下游。
我正在尝试映射以下内容:
表:
BaseTable
{
int PK1;
int PK2;
string? Value1;
double? Value2;
}
ChildTable3
{
int PK1;
int PK2;
int Value;
}
实体:
abstract BaseEntity : class // Maps to BaseTable
{
int PK1; // Maps to PK1 in relevant table
int PK2; // Maps to PK2 in relevant table
}
Entity1 : BaseEntity // Maps to BaseTable when Value1 != null
{
string Value; // Maps to Value1
}
Entity2 : BaseEntity // Maps to BaseTable when Value1 == null && Value2 != null
{
double Value; // Maps to value2
}
Entity3 : BaseEntity // Maps to ChildTable3
{
int Value; // Maps to value
}
在添加 Entity3 映射之前有效。
添加 Entity3 及其映射后,我在编译时收到以下错误:
错误 1 错误 3026:从第 980、986、995 行开始的映射片段出现问题:表 BaseTable 中可能出现数据丢失或键约束冲突。 具有密钥 (PK) 的实体在以下情况下不会往返: (PK 位于“BaseTables”EntitySet 中,并且实体的类型为 [MyNamespace.Entity3]) Path\To\My.edmx 981 15 MyAssembly
- 有办法让它工作吗?
- 如果可以破解 edmx 来实现此功能,我是否会在数据库的每次更新上都失去破解功能?
First of all, these questions are similar but definitely not the same:
Can I mix Table per Hierarchy and Table per Type in Entity Framework? - Refers to a different scenario.
Entity Framework: mix table per type and table per hierarchy - Yet another scenario, that despite accepting answer to first scenario is not related to it (*).
(*) Second of all, I have sucessfully mixed table-per-type and table-per-hierarchy in Entity Framework, when base entity is mapped using table-per-entity and the table with the discriminator of the table-per-hierarchy is lower down the chain.
I am trying to map the following:
Tables:
BaseTable
{
int PK1;
int PK2;
string? Value1;
double? Value2;
}
ChildTable3
{
int PK1;
int PK2;
int Value;
}
Entities:
abstract BaseEntity : class // Maps to BaseTable
{
int PK1; // Maps to PK1 in relevant table
int PK2; // Maps to PK2 in relevant table
}
Entity1 : BaseEntity // Maps to BaseTable when Value1 != null
{
string Value; // Maps to Value1
}
Entity2 : BaseEntity // Maps to BaseTable when Value1 == null && Value2 != null
{
double Value; // Maps to value2
}
Entity3 : BaseEntity // Maps to ChildTable3
{
int Value; // Maps to value
}
Prior to adding Entity3 mappings worked.
After adding Entity3 and its mapping, I receive the following error while compiling:
Error 1 Error 3026: Problem in mapping fragments starting at lines 980, 986, 995:Data loss or key constraint violation is possible in table BaseTable.
An Entity with Key (PK) will not round-trip when:
(PK is in 'BaseTables' EntitySet AND Entity is type [MyNamespace.Entity3])
Path\To\My.edmx 981 15 MyAssembly
- Is there a way to make this work?
- If it is possible to hack the edmx to make this work, will I loose the hack on every update from database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个工作流程,可以通过设计器执行此类映射:
稍后添加其他子表:
I found a workflow that enables performing such mappings via designer:
To add additional child tables later on: