代码优先的外键异常让我发疯

发布于 2024-12-15 12:07:45 字数 1437 浏览 1 评论 0原文

这些是我的模型类:

public class Organization
{
        public Organization()
        {
        }

        [DisplayName("Organization Id")]
        public int OrganizationId { get; set; }

        [StringLength(128)]
        [DisplayName("Organization Name")]
        public string Name { get; set; }
}

public class User 
{
        public User()
        {
            Roles = new List<Role>();
        }

        [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
        [Key]
        public Guid UserGuid { get; set; }

        [StringLength(25)]
        public string FirstName { get; set; }

        [StringLength(25)]
        public string LastName { get; set; }

        public virtual List<Role> Roles { get; set; }

        public int OrganizationId { get; set; }

        [ForeignKey("OrganizationId")]
        public virtual Organization Organization { get; set; }
    }

这是我的代码:

 Organization organization = new Organization { Name = "Test", };
 context.Organizations.Add(organization);

我得到这个:

INSERT 语句与 FOREIGN KEY 约束冲突 \“组织_用户\”。数据库发生冲突 \“SampleDB \”,表\“dbo.Organizations \”,列 “OrganizationId”。\r\n该语句已终止。

这不是很奇怪吗?我只是添加一个组织。这其中可能存在什么问题呢?

PS:我的用户表确实有OrganizationId,它是外键并指向Organization表。到目前为止一切顺利,但为什么会抛出异常?我正在添加主组织记录。这如何违反外键约束?

These are my model classes:

public class Organization
{
        public Organization()
        {
        }

        [DisplayName("Organization Id")]
        public int OrganizationId { get; set; }

        [StringLength(128)]
        [DisplayName("Organization Name")]
        public string Name { get; set; }
}

public class User 
{
        public User()
        {
            Roles = new List<Role>();
        }

        [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
        [Key]
        public Guid UserGuid { get; set; }

        [StringLength(25)]
        public string FirstName { get; set; }

        [StringLength(25)]
        public string LastName { get; set; }

        public virtual List<Role> Roles { get; set; }

        public int OrganizationId { get; set; }

        [ForeignKey("OrganizationId")]
        public virtual Organization Organization { get; set; }
    }

This is my code:

 Organization organization = new Organization { Name = "Test", };
 context.Organizations.Add(organization);

And I get this:

The INSERT statement conflicted with the FOREIGN KEY constraint
\"Organization_Users\". The conflict occurred in database
\"SampleDB\", table \"dbo.Organizations\", column
'OrganizationId'.\r\nThe statement has been terminated.

Isn't this weird? I am just adding an organization. What problem can it possibly have in this?

P.S: My user table does have OrganizationId that is foreign key and pointing to Organization table. So far so good but why is the exception thrown? I am adding a master Organization record. How does that violate foreign key constraint?

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

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

发布评论

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

评论(2

薄凉少年不暖心 2024-12-22 12:07:45

我希望也为组织生成数据库 ID:

   [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
   [DisplayName("Organization Id")]
   public int OrganizationId { get; set; }

要么这样,要么您需要在保存之前自行将其设置为有效的唯一值

替代猜测:

名称 Organization_Users 表明存在(或曾经)从组织表到用户的关系(可能通过关系表)。您显示了完整的代码吗?数据库中的代码是否有这种旧关系的残余?仅从 C# 代码中删除此类关系是不够的(因为数据库将继续检查约束,直到从实际数据库模式中删除未使用的字段/关系)。

I would expect the database id to be generated for the organisation as well:

   [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
   [DisplayName("Organization Id")]
   public int OrganizationId { get; set; }

Either that, or you need toset it to a valid, unique value yourself before saving

Alternative guess:

The name Organization_Users suggests that there is (or was) a relation from the Organization table to th Users (perhaps via a relation table). Did you show the full code? Code there be remnants of this old relationship in the database? It is not enough to remove such relations just from the C# code (because the datatabase will continue checking the constraints, until the unused fields/relations are dropped from the actual database schema).

自控 2024-12-22 12:07:45

EF 似乎以错误的方向创建了关系。

User 类应该有一个 Organization 而不是 OrganizationID。

Organization 类应该有一个用户列表。

It appears that EF has created the relationship in the wrong direction.

The User class should have an Organization not an OrganizationID.

The Organization class should have a list of users.

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