在非键列上创建实体关联

发布于 2024-11-03 13:14:02 字数 523 浏览 0 评论 0原文

我有两个表“用户”和“学校”。每个都有主键,没有外键。我必须绑定的数据库根本没有标准化,但我仍然必须在其中建立关联。 我注意到其他一些类似的问题,但我不太明白他们在问什么,如果这是重复的,很抱歉。

Users Table
UserId int NON NULL
School varchar NON NULL

Schools Table
SchoolId int NON NULL
SchoolName varchar NON NULL

数据如下所示:

Users Table
UserId    School
1         ABC
2         ABC

Schools Table
SchoolId  SchoolName
1         ABC
2         DEF

我要创建的关联如下所示,键是 Schools 表中的 SchoolName 和 Users 表中的 School

User *--1 School

I have two tables Users and Schools. Each have primary keys and no foreign keys. The database I have to tie in has no normalization at all, but I still have to have associations in it.
I noted some other questions that were similar, but I couldn't quite understand what they were asking, so sorry if this is a repeat.

Users Table
UserId int NON NULL
School varchar NON NULL

Schools Table
SchoolId int NON NULL
SchoolName varchar NON NULL

The data looks like this:

Users Table
UserId    School
1         ABC
2         ABC

Schools Table
SchoolId  SchoolName
1         ABC
2         DEF

The association I would like to create is below, with the key being the SchoolName in the Schools table and the School in the Users table

User *--1 School

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的影子我的梦 2024-11-10 13:14:02

不幸的是这是不可能的。如果您想在数据库中执行此操作,您将在 Schools 表中将 SchoolName 标记为唯一(通过放置唯一约束/索引),因此您将能够构建一对多的关系。 EF 不支持唯一键,因为您无法在数据库中定义此类关联(它不会反映在您的模型中)或通过引用约束直接在模型中定义。

使用 EF 时,良好的数据库设计仍然是关键。

Unfortunately this is not possible. If you want to do this in the database you will mark SchoolName in Schools table as unique (by placing unique constraint / index) and because of that you will be able to build one-to-many relation. EF doesn't support unique keys any because of that you cannot define such association neither in database (it will not be reflected in your model) or in the model directly by referential constraints.

When working with EF the good database design is still the key.

要走干脆点 2024-11-10 13:14:02

仅供有类似问题的人参考,Linq to SQL 确实具有此功能,并且看起来工作得很好。

As a FYI to someone having a similar issue, Linq to SQL does have this ability and it appears to work just fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文