实体框架无法识别的唯一密钥

Dav*_*fer 8 .net c# sql entity-framework entity-framework-4

我有两张桌子,ReportsVisualizations.Reports有一个字段,通过外键VisualizationID指向Visualization同名的字段.它还具有在现场声明的唯一键.VisualizationID不可空.这意味着关系必须为0..1到1,因为每个 Reports记录必须具有Visualizations与之关联的唯一非空记录.

实体框架不会这样看待它.我收到以下错误:

Error 113: Multiplicity is not valid in Role 'Report' in relationship 'FK_Reports_Visualizations'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *.

这有什么问题?如何让EF识别正确的关系多样性?

Ale*_*mes 3

EF 正在抱怨,因为听起来您正在使用 FK 关联 - 这意味着 VisualizationID 是实体的属性,并且也有可视化引用 - 而您不能使用 FK 关联来执行此操作。

但是,如果您使用独立关联(这意味着没有 VisualizationID 属性),您可以缩小基数。

因此,解决方案是从实体中删除 VisualizationID 属性,此时您需要继续映射关联。

希望这可以帮助

亚历克斯