gpt4 book ai didi

c# - Entity Framework 代码优先 - 非主键字段的外键

转载 作者:太空狗 更新时间:2023-10-29 21:41:22 25 4
gpt4 key购买 nike

我有两个表格,如下所示:

dbo.ReviewType
ReviewTypeId INT PRIMARY KEY
ShortName CHAR(1) - Unique Index
Description

dbo.Review
ReviewId INT PRIMARY KEY
ReviewType_ShortName CHAR(1) - FK to ReviewType
...

评论总是有一个评论类型。
一个 ReviewType 可以与许多评论相关联。

我在使用 Code First Fluent API 将其映射到 Entity Framework 时遇到了问题。它似乎不喜欢我使用未映射到主键的外键。我将外键用于唯一约束/索引而不是主键。

如何使用 C# 在 Entity Framework 中正确映射它?

我应该注意,我现在这样做的方式给我这个错误:

System.Data.Edm.EdmAssociationConstraint: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'ReviewTypeCode' on entity Review' does not match the type of property 'Id' on entity 'ReviewType' in the referential constraint 'ReviewType_Reviews'.

最佳答案

当前版本的 EF 不支持唯一索引,它不能映射基于非主唯一键的关系。

关于c# - Entity Framework 代码优先 - 非主键字段的外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7488300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com