假设我有 2 个表:Message 和 SuperMessage
和 3 个实体:Message(基础(非抽象))、Comment(继承自 Message)和 SuperMessage(继承自 Message)
消息有一个不可为空的 MessageType 字段,用作鉴别器。
问题是我无法在 SuperMessage 的 Mapping details 部分的 MessageType 上指定条件,因为它看不到 MessageType 字段,我无法忽略它。
我怎样才能让这些工作相互配合?
更新 构建错误:
错误 3014:映射片段中的问题:模型中未强制执行从表 SuperMessage (ID) 到表 Message (MessageId) 的外键“外键约束“FK_SuperMessage_inherits_Message”。需要创建关联或继承关系来强制执行此约束。
最佳答案
您是否尝试过添加中间抽象实体类型,即:
abstract MessageBase --> Message table
non-abstract Message --> Message table when MessageType == 1
non-abstract Comment --> Message table when MessageType == 2
abstract SuperMessageBase --> Message table when MessageType == 3
non-abstract SuperMessage --> SuperMessage table
另请查看我解决的类似场景:
EF: Can I mix TPH and TPT when abstract base and a few concrete types are in TPH-table and other types have their own table?
关于entity-framework - 我可以在 Entity Framework 中混合使用每个层次结构的表和每个类型的表吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4605892/