在非键列上创建实体关联
我有两个表“用户”和“学校”。每个都有主键,没有外键。我必须绑定的数据库根本没有标准化,但我仍然必须在其中建立关联。 我注意到其他一些类似的问题,但我不太明白他们在问什么,如果这是重复的,很抱歉。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是这是不可能的。如果您想在数据库中执行此操作,您将在
Schools
表中将SchoolName
标记为唯一(通过放置唯一约束/索引),因此您将能够构建一对多的关系。 EF 不支持唯一键,因为您无法在数据库中定义此类关联(它不会反映在您的模型中)或通过引用约束直接在模型中定义。使用 EF 时,良好的数据库设计仍然是关键。
Unfortunately this is not possible. If you want to do this in the database you will mark
SchoolName
inSchools
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.
仅供有类似问题的人参考,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.