不是由实体框架认可的唯一钥匙 [英] Unique Keys not recognized by Entity Framework

查看:373
本文介绍了不是由实体框架认可的唯一钥匙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,报告可视化报告有一个字段, VisualizationID ,它指向可视化同名通过一个外键的字段。它还具有对字段声明一个唯一的密钥。 VisualizationID 不能为空。这意味着亲密关系要0..1〜1,因为 报告记录必须有一个唯一的,非空可视化记录与它相关联。

I have two tables, Reports and Visualizations. Reports has a field, VisualizationID, which points to Visualization's field of the same name via a foreign key. It also has a unique key declared on the field. VisualizationID is not nullable. This means the relationship has to be 0..1 to 1, because every Reports record must have a unique, not null Visualizations record associated with it.

实体框架并不这样看。我收到以下错误:

The Entity Framework doesn't see it this way. I'm getting the following error:

错误113:多重不中关系FK_Reports_Visualizations角色报告有效。由于从属角色属性都不是关键属性,上限从属角色的多重性必须是*。

有什么问题吗?我怎样才能使EF识别的正确关系的多重性?

What's the problem here? How can I make the EF recognize the proper relationship multiplicity?

推荐答案

该EF抱怨,因为它听起来像您使用的是FK协会 - 这意味着VisualizationID是实体的属性,有一个可视化的参考太 - 你不能用FK协会为此

The EF is complaining because it sounds like you are using an FK Association - which means that the VisualizationID is a property of the Entity and there is a Visualization reference too - and you can't do this with FK Associations.

然而,如果您使用独立的关联 - 这意味着没有VisualizationID财产 - 你的可以的缩小基数

If however you use Independent Associations - which means there is no VisualizationID property - you can narrow the cardinality.

所以,解决的办法是从实体中删除VisualizationID属性,此时你需要继续前进的映射关系。

So the solution is to remove the VisualizationID property from the Entity, at which point you need to go ahead an map the association.

希望这有助于

亚历

这篇关于不是由实体框架认可的唯一钥匙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆