ASP.NET MVC - 外键作为主键?

发布于 2024-12-08 12:22:37 字数 548 浏览 1 评论 0原文

是否可以将外键作为主键? 如果是这样,有人可以帮助我处理我的代码吗?

<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 技术交流群。

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

发布评论

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

评论(3

別甾虛僞 2024-12-15 12:22:37

如果您尝试创建一个每个分配有一条(或没有)记录的表,那么如何使该表中多行的AssignmentID 唯一?您应该创建一个具有唯一的AssignmetnID 的表,然后为每个分配的多个记录创建AssignmetnRecordID。

示例代码是 C# 语言,因为我更熟悉 C# 语法。

你应该创建一个像这样的数据模型,

public class xyz
{
    public int AssignmentID { get; set; }

    // Some other properties

    [ForeignKey("AssignmetnRecordID")]
    public virtual ICollection<Assignment> AssignmentRecords { get; set; }
}

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,

public class xyz
{
    public int AssignmentID { get; set; }

    // Some other properties

    [ForeignKey("AssignmetnRecordID")]
    public virtual ICollection<Assignment> AssignmentRecords { get; set; }
}
泡沫很甜 2024-12-15 12:22:37

只需将赋值添加为属性就足够了(对不起,C# 语法):

public class Xyz
{
    public int XyzID { get; set; }

    public int AssignmentId { get; set; }

    [ForeignKey("AssignmentId")]
    public virtual Assignment Assignment { get; set; }
}

public class Assignment
{
    public int AssignmentID { get; set; }

}

XyzID 和AssignmentID 可能不相同,但它只允许 0 或 1 个赋值。

It should be enough to just add the assignment as a property (sorry C# syntax):

public class Xyz
{
    public int XyzID { get; set; }

    public int AssignmentId { get; set; }

    [ForeignKey("AssignmentId")]
    public virtual Assignment Assignment { get; set; }
}

public class Assignment
{
    public int AssignmentID { get; set; }

}

XyzID and AssignmentID would probably not be the same, but it allows for only 0 or 1 assignment.

陌上青苔 2024-12-15 12:22:37

是的,这是可能的。一个常见的示例是一个表,用于实现两个表之间的多对多关系。例如,角色和用户将具有多对多关系,两个 ID 的组合将构成一个很好的主键。
诀窍是 DatabaseGenerate 属性,它允许您告诉 EF 您将生成键的值,而不是像 Identity int 字段那样生成数据库。

public class RoleUsers
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None), Column(Order = 0)]
    public int          RoleId { get; set; }
    public virtual Role Role   { get; set; }

    [Key, DatabaseGenerated(DatabaseGeneratedOption.None), Column(Order = 1)]
    public Guid UserGuid { get; set; }
}

但是,在您的情况下,您需要 0 或 1 关系,因此您应该可以

public class MyClass
{
    public int Id { get; set; }

    public int? AssignmentId { get; set; }
    public virtual Assignment Assignment { get; set; }
    ...
}

为 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.

public class RoleUsers
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None), Column(Order = 0)]
    public int          RoleId { get; set; }
    public virtual Role Role   { get; set; }

    [Key, DatabaseGenerated(DatabaseGeneratedOption.None), Column(Order = 1)]
    public Guid UserGuid { get; set; }
}

However, in your case, you want a 0 or 1 relationship so you should

public class MyClass
{
    public int Id { get; set; }

    public int? AssignmentId { get; set; }
    public virtual Assignment Assignment { get; set; }
    ...
}

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.

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