【发布时间】:2016-08-24 15:50:08
【问题描述】:
我有一个 entityframewrok 的问题,它将两个表之间的 1:0..1 关系序列化为一个集合。
我的数据库中有 2 个表:
CREATE TABLE `revisiones` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
---Irrelevant columns here---
PRIMARY KEY (`Id`),
---Irrelevant constraint and foreign keys here---
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `ficha_deposito` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`IdRevision` int(11) NOT NULL,
---Irrelevant Columns Here---
PRIMARY KEY (`Id`),
UNIQUE KEY `IdRevision_UNIQUE` (`IdRevision`),
CONSTRAINT `fk_ficdep_rev` FOREIGN KEY (`IdRevision`) REFERENCES `revisiones` (`Id`) ON DELETE CASCADE ON UPDATE CASCADE,
---Irrelevant constraints here---
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
如您所见,由于独特的限制,修订可能与 none 或 1 ficha_deposito 相关。
然而在 edmx 文件中,关系被序列化为一个集合:
如果我尝试手动更改它(我宁愿不这样做,因为如果我必须重新生成模型,我将不得不再次手动设置该值),然后我得到一个异常:
Running transformation: Multiplicity is not valid in Role 'ficha_deposito' in relationship 'fk_ficdep_rev'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *.
为什么我不能改变关系的多重性? revision.ficha_deposito 应该是 null 或一个简单的对象。
【问题讨论】:
-
这是因为 EF6 不支持
one-to-oneFK 关联。看看One-to-One Foreign Key Associations和Shared Primary Key Associations
标签: c# mysql entity-framework foreign-keys