简体   繁体   English

EF 7:INSERT语句与FOREIGN KEY SAME TABLE冲突

[英]EF 7 : INSERT statement conflicted with the FOREIGN KEY SAME TABLE

I have an exception when I call SaveChangesAsync. 当我调用SaveChangesAsync时,我有一个例外。 My architecture is really simple, i have a Category class, wich contains 我的架构非常简单,我有一个类别,包含

public class Category {
  public Guid ID {get; set;}
  public Guid? ParentId {get; set;}
  public Category Parent {get; set;}
 [...]
}

When I want to insert a new category in database (connected to my ASP MVC application), I set the GUID before doing the insert. 当我想在数据库中插入一个新类别(连接到我的ASP MVC应用程序)时,我在执行插入之前设置了GUID。 The error occured when my database is empty and I want to insert parent category (so with a null Guid IdParent and null Parent). 当我的数据库为空并且我想插入父类别时发生错误(所以使用null Guid IdParent和null Parent)。 This is NOT happening if I set a parent value. 如果我设置父值,则不会发生这种情况。 I can add a record manually by setting parent to Null by Visual studio. 我可以通过Visual Studio将父设置为Null来手动添加记录。

I have the following error : 我有以下错误:

The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK_Category_Category_ParentId". INSERT语句与FOREIGN KEY SAME TABLE约束“FK_Category_Category_ParentId”冲突。 The conflict occurred in database "HT_Root", table "dbo.Category", column 'ID'. 冲突发生在数据库“HT_Root”,表“dbo.Category”,列“ID”中。 The statement has been terminated. 该语句已终止。

I searched on stack overflow for a simple answer, and not found it. 我在堆栈溢出搜索了一个简单的答案,但没有找到它。 I try with Fluent API : 我尝试使用Fluent API:

modelBuilder.Entity<Category>().HasOne(s => s.Parent).WithMany().HasForeignKey(s => s.ParentId);

But nothing changed. 但没有改变。 What am I doing wrong ? 我究竟做错了什么 ?

It seems to have changed in EF 7 See this github issue 它似乎在EF 7中发生了变化。 请参阅此github问题

Try 尝试

public class Category 
{
    public Guid ID {get; set;}
    public Guid? ParentId {get; set;}
    public Category Parent {get; set;}
    public ICollection<Categories> Children {get; set;}
}

And

modelBuilder.Entity<Category>()
    .HasOne(x => x.Parent)
    .WithMany(x => x.Children)
    .HasForeignKey(x => x.ParentId)
    .Required(false);

You should also always check that (if specified) the ParentId exists in the database. 您还应始终检查数据库中是否存在ParentId(如果已指定)。 Watch out for adding Guid.Empty (00000000-0000-0000-0000-000000000000) instead of null as this can cause issues. 注意添加Guid.Empty (00000000-0000-0000-0000-000000000000)而不是null因为这可能会导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF核心中的自引用错误:插入语句与外键同表约束冲突 - Self-referencing in EF core Error: the insert statement conflicted with the foreign key same table constraint EF Core:INSERT语句与FOREIGN KEY约束冲突 - EF Core: The INSERT statement conflicted with the FOREIGN KEY constraint INSERT语句与EF Core中的FOREIGN KEY约束冲突 - The INSERT statement conflicted with the FOREIGN KEY constraint in EF Core EF Core 7:INSERT 语句在 SAVECHANGES 期间与外键约束冲突 - EF Core 7: The INSERT statement conflicted with the foreign key constraint during SAVECHANGES INSERT 语句与使用 EF Core 的 FOREIGN KEY 冲突 - The INSERT statement conflicted with the FOREIGN KEY using EF Core EF Code First INSERT 语句与 FOREIGN KEY 约束冲突 - EF Code First The INSERT statement conflicted with the FOREIGN KEY constraint INSERT语句与FOREIGN KEY冲突 - The INSERT statement conflicted with the FOREIGN KEY INSERT语句与FOREIGN KEY冲突 - INSERT statement conflicted with the FOREIGN KEY 如何修复:UPDATE语句与FOREIGN KEY SAME TABLE约束冲突 - How to fix: The UPDATE statement conflicted with the FOREIGN KEY SAME TABLE constraint BulkInsert:INSERT语句与FOREIGN KEY约束冲突 - BulkInsert: The INSERT statement conflicted with the FOREIGN KEY constraint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM