身份唯一索引作为外键 [英] identity unique index as foreign key

查看:115
本文介绍了身份唯一索引作为外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我是实体框架的新手。我创建的小型数据库中的几个表都使用了复合键。我不想将这些复合键用作外键,因此我创建了标识列并在列上添加了唯一索引,并在数据库中用作外键。但是这种设计在实体模型中不起作用,因为我只使用主键。有什么方法可以使它工作吗?

这里简单的例子

表A

工作Id主键

职位描述

表B

作业ID主键

Job 名称

Job  name

表C

Job_id_A主键

Job_id_B主键

匹配id(身份 唯一索引)

Match id (identity  unique index)

表D

Evalid

Evel date

Match Id

某些字段

< span style ="font-size:small"> ... ..

推荐答案

您好,

正如您已经提到的,实体框架目前无法推断不是主键的唯一键。在某些情况下,可以告诉实体框架"谎言"。关于数据库中的模式。例如,您可以标记"匹配ID"。如表C的PK,然后手动控制表C与表B和表A的关系的多样性,但我不认为这在这种情况下会有所帮助。

如果您可以将匹配ID输出,EF可以使用表C中的复合键进行管理。

希望这有帮助,
迭戈
Hello,

As you have already mentioned, Entity Framework does not currently have the capability to reason about unique keys that are not primary keys. It is possible in certain cases to tell  Entity Framework "lies" about the schema in your database. For instance, you could mark "Match id" as Table C's PK, and then manually control the multiplicity of the relationships of Table C with Table B, and Table A, but I don't think this would help in this case.

EF can manage ok with the composite key in Table C if you are ok with leaving Match id out.

Hope this helps,
Diego


这篇关于身份唯一索引作为外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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