ASP.NET MVC - 外键作为主键?
是否可以将外键作为主键? 如果是这样,有人可以帮助我处理我的代码吗?
<Key()>
Public Property AssignmentID() As Integer
<ForeignKey("AssignmentID")>
Public Overridable Property Assignment As Assignment
Public Overridable Property User As User
对于上面的代码我得到一个错误:
模型期间检测到一个或多个验证错误 一代:
System.Data.Edm.EdmAssociationEnd: : 多重性在以下位置无效 关系中的角色“AssignmentLocks_Assignment_Source” 'AssignmentLocks_Assignment'。因为从属角色指的是 关键属性,从属的多重性的上限 角色必须是“1”。
我正在尝试创建一个表,其中每个作业有一个(或没有)记录。
Is it possible to have a Foreign Key as the Primary Key?
If so, can someone help me with my code?
<Key()>
Public Property AssignmentID() As Integer
<ForeignKey("AssignmentID")>
Public Overridable Property Assignment As Assignment
Public Overridable Property User As User
For the code above I get an error:
One or more validation errors were detected during model
generation:System.Data.Edm.EdmAssociationEnd: : Multiplicity is not valid in
Role 'AssignmentLocks_Assignment_Source' in relationship
'AssignmentLocks_Assignment'. Because the Dependent Role refers to the
key properties, the upper bound of the multiplicity of the Dependent
Role must be �1�.
I'm trying to create a table which has one (or none) record per assignment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您尝试创建一个每个分配有一条(或没有)记录的表,那么如何使该表中多行的AssignmentID 唯一?您应该创建一个具有唯一的AssignmetnID 的表,然后为每个分配的多个记录创建AssignmetnRecordID。
示例代码是 C# 语言,因为我更熟悉 C# 语法。
你应该创建一个像这样的数据模型,
if you are trying to create a table which has one (or none) record per assignment then how do you have AssignmentID unique in that table for multiple rows? You should create a table with unique AssignmetnID and then AssignmetnRecordID for those multiple records per assignment.
Example code is in C# as I am more familiar with C# syntax.
you should be creating a datamodel something like this,
只需将赋值添加为属性就足够了(对不起,C# 语法):
XyzID 和AssignmentID 可能不相同,但它只允许 0 或 1 个赋值。
It should be enough to just add the assignment as a property (sorry C# syntax):
XyzID and AssignmentID would probably not be the same, but it allows for only 0 or 1 assignment.
是的,这是可能的。一个常见的示例是一个表,用于实现两个表之间的多对多关系。例如,角色和用户将具有多对多关系,两个 ID 的组合将构成一个很好的主键。
诀窍是 DatabaseGenerate 属性,它允许您告诉 EF 您将生成键的值,而不是像 Identity int 字段那样生成数据库。
但是,在您的情况下,您需要 0 或 1 关系,因此您应该可以
为 null int (int?) 允许 0 或 1 关系,并且生成的列可为空。如果它只是 int,则该列不可为空,并且必须有一个关联的赋值。
Yes it is possible. One common example would be a table to implement a many-to-many relationship between two tables. For example Roles and User would have a many-to-many relationship and the combination of the two IDs would make a good primary key.
The trick is the DatabaseGenerate attribute which allow you t tell EF that you will generate the value for the key, not the database as is the case for identity int fields.
However, in your case, you want a 0 or 1 relationship so you should
THe nullable int (int?) allows the 0 or one relationship and the generated column is nullable. If it is just int, then the column is not nullable and there must be an associated Assignement.