实体框架和超类型/子类型
根据我在最近的一个问题上收到的极好的建议(数据库设计问题), 我正在将超类型/子类型模式合并到数据库中 为 MVC2 应用程序构建。 我将使用实体框架向 MVC 提供模型。
超类型是出版物,而子类型是文章, 博客文章等 每个子类型表都会有一个2列的复合主键(pub_id, pub_type),具有引用中相应列的外键 超类型表
出于数据完整性目的(请参阅对已接受解决方案的冗长评论 对于原始问题)超类型表不应包括 pub_type 在其主键中(与子类型表不同),
这就是实体框架似乎不配合的地方。从现有数据库生成 .edmx 文件时收到的警告:
关系“FK_Articles_Publications”包含不属于 关系主侧的表键的一部分。关系被排除。
(等)
问题: 有什么方法可以诱使实体框架映射给定的关系 我想要的外键(或者我是否必须妥协数据库设计和 在超类型表上设置复合键)?
如果不是,这会增加额外的问题,要求加入任何 其他非子类型表(我计划有几个)到超类型 表要求我有对应于 pub_id 和 pub_type。例如,我想要一个主题表能够关联 通过超类型表进行任何类型的发布——我需要 将 pub_type 存储(冗余)在主题表的列中。
我对 EF(和 ORM)还很陌生,但它的力量很诱人,我不想放弃它。
Per excellent advice I received on a recent question (Database design problem),
I am incorporating a super-type/sub-type pattern in a DB I'm
building for an MVC2 app.
I'll be using Entity Framework to provide the models to MVC.
The super-type is Publications, while the sub-types are Articles,
BlogPosts, etc.
Each sub-type table will have a 2-column composite primary key (pub_id,
pub_type), with foreign keys that reference corresponding columns in the
super-type table
For data integrity purposes (see lengthy comments on accepted solution
to original question) the super-type table should not include the
pub_type in its primary key (unlike the sub-type tables),
And that is where Entity Framework doesn't seem to play along. Warnings I get when I generate the .edmx file from my existing database:
The relationship 'FK_Articles_Publications' has columns that are not
part of the key of the table on the priamry side of the relationship. The relationship was excluded.
(etc.)
QUESTION:
is there any way to coax Entity Framework into mapping the relationship given
the foreign keys I want (or will I have to compromise the DB design and
set a composite key on the supertype table)?
If not, this adds the additional problem of requiring that joing any
other, non-sub-type table (and I plan on several) to the super-type
table requires that I have columns corresponding to both pub_id and
pub_type. For example, I want a topics table to be able to associate
with any kind of publication via the super-type table -- I would need
to store (redundantly) the pub_type in a column in the topics table.
I am quite new to EF (and ORM), but it's power is alluring, and I don't want to give it up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在此表中,
重要的是 {pub_id} 是唯一的并且 {pub_id, pub_type} 是唯一的。 (pub_type 列也应声明为
NOT NULL
。)在第一种情况下,唯一性保证了同一性。在第二种情况下,唯一性保证子类型引用超类型中正确类型的行。但对于数据库引擎来说,哪一个被声明为PRIMARY KEY
以及哪一个被声明为UNIQUE
并不重要。两者都可以作为外键引用的目标。如果数据库管理系统是围绕当前关系理论设计的,它可能仅支持
KEY
声明,而不是PRIMARY KEY
和NOT NULL UNIQUE
。因此,如果您的 ORM 无法按照编写的方式处理约束,则可以将 {pub_id} 设置为所有这些表中的主键,并声明可简化的超级键 {pub_id, pub_type} 在所有这些表中是唯一的。
我不知道这是否会让EF沉默。您应该测试引用子类型的表以及引用超类型的表。
In this table,
it matters that {pub_id} is unique and that {pub_id, pub_type} is unique. (The column pub_type should also be declared
NOT NULL
.) In the first case, uniqueness guarantees identity. In the second case, uniqueness guarantees that subtypes reference the right kind of row in the supertype. But it doesn't matter much to the database engine which one is declaredPRIMARY KEY
and which one is declaredUNIQUE
. Both can be the target of foreign key references.If a dbms were designed around current relational theory, it might support only
KEY
declarations instead ofPRIMARY KEY
andNOT NULL UNIQUE
.So if your ORM can't cope with constraints the way they're written, it's ok to make {pub_id} the primary key in all those tables, and to declare the reducible superkey {pub_id, pub_type} to be unique in all those tables.
I don't know whether that will silence EF. You should test tables that reference the subtypes as well as tables that reference the supertype.
尝试每个层次结构的表继承,看起来它很适合你的设计。
Try the Table Per Hierarchy inheritance, it looks like it suits your design rather well.