代码优先的外键异常让我发疯
这些是我的模型类:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我希望也为组织生成数据库 ID:
要么这样,要么您需要在保存之前自行将其设置为有效的唯一值
替代猜测:
名称
Organization_Users
表明存在(或曾经)从组织表到用户的关系(可能通过关系表)。您显示了完整的代码吗?数据库中的代码是否有这种旧关系的残余?仅从 C# 代码中删除此类关系是不够的(因为数据库将继续检查约束,直到从实际数据库模式中删除未使用的字段/关系)。I would expect the database id to be generated for the organisation as well:
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).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.