实体框架 0..1 到 0..1 关联
这可能吗?
我有用户和提供者实体。用户可能是也可能不是提供商(可能只是助理),提供商可能是也可能不是用户(可能是位置)。
设计师让我对此进行建模,但由于关键属性,它不会验证。用EF可以实现吗?
Is this possible?
I have User and Provider entities. A User may or may not be a Provider (could just be an Assistant) and a Provider may or may not be a User (it could be a Location).
The designer lets me model this, but it won't validate because of the key properties. Is this possible to achieve with EF?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这不是因为 EF 中的一对一关系始终通过主键建模。从属实体必须在其主键上设置对主体实体的外键约束。它总是导致 0..1 到 1,其中主体可以有一个依赖项或没有依赖项,但依赖项必须有一个主体,因为它必须有一个主键值,并且该值对主体实体具有引用约束。
顺便提一句。我不确定这是否可以在数据库中建模,因为即使使用唯一键,空值仍然被视为唯一值之一,因此当您在该列上也有唯一索引时,只有单个记录可以将值设置为空。
No it is not because one-to-one relation in EF is always modeled through the primary key. Dependent entity must have foreign key constraint to principal entity placed on its primary key. It always result in 0..1 to 1 where principal can have one or none dependent but dependent must have a principal because it must have a primary key value and that value has referential constraint to principal entity.
Btw. I'm not sure if this can be even modeled in the database because even with unique key null value is still considered as one of unique values so only single record can have a value set to null when you also have unique index on that column.