简体   繁体   English

INSERT 语句与 net core 中的 FOREIGN KEY 约束冲突

[英]The INSERT statement conflicted with the FOREIGN KEY constraint in net core

I have 2 tables.我有 2 张桌子。 one for Courses and another one for Keywords.一个用于课程,另一个用于关键字。 These two table have many-to-many relations.这两个表具有多对多关系。 So I put another table as CourseKeywords .所以我把另一个表作为CourseKeywords

public class CourseKeyword
{
    public int CourseId { get; set; }
    public int KeywordId { get; set; }
    public virtual Course Course { get; set; }
    public virtual Keyword Keyword { get; set; }
}

public class Course
{
    [Key]
    public int Id { get; set; }
    [MaxLength(300)]
    public string Title { get; set; }
    [MaxLength(300)]
    public string SubTitle { get; set; }
    public string Body { get; set; }
   
    public ICollection<CourseKeyword> Keywords { get; set; }   

    public Guid Token { get; set; }
    [MaxLength(300)]
    public string Photo { get; set; }
    [MaxLength(300)]
    public string Preview { get; set; }
    public bool IsSpecial { get; set; }
}

public class Keyword
{
    [Key]
    public int Id { get; set; }
    [MaxLength(300)]
    public string Title { get; set; }
    public ICollection<CourseKeyword> CourseKeywords { get; set; }
    public ICollection<BlogKeyword> BlogKeywords { get; set; }
}

When I want to add a new course with some new keywords.当我想添加带有一些新关键字的新课程时。 I successfully add course.我成功添加了课程。 Then add keyword, but when I try to add CourseKeyword object, at SaveChange() it shows below error:然后添加关键字,但是当我尝试添加CourseKeyword object 时,在SaveChange()处显示以下错误:

InnerException = {"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_CourseKeywords_Course_KeywordId". The conflict occurred in database "PandaAcademyNew", table "dbo.Course", column 'Id'.\r\nThe statement has been terminated."} InnerException = {“INSERT 语句与 FOREIGN KEY 约束“FK_CourseKeywords_Course_KeywordId”冲突。冲突发生在数据库“PandaAcademyNew”、表“dbo.Course”、列“Id”中。\r\n语句已终止。”}

This happen when I want to save change.当我想保存更改时会发生这种情况。 My code is here:我的代码在这里:

var course = _db.Course.Find(model.Id);
var keyw = _db.Keywords.Find(keyword.Id);
    
var key = new CourseKeyword()
{
    Course = course,
    Keyword = keyw
};

_db.CourseKeywords.Add(key);
_db.SaveChanges();

This is my context OnModelCreating :这是我的上下文OnModelCreating

builder.Entity<CourseKeyword>(courseKeyword =>
{
    courseKeyword.HasKey(ur => new { ur.KeywordId, ur.CourseId });

    courseKeyword.HasOne(ur => ur.Course)
        .WithMany(r => r.Keywords)
        .HasForeignKey(ur => ur.CourseId)
        .IsRequired();

    courseKeyword.HasOne(ur => ur.Keyword)
        .WithMany(r => r.CourseKeywords)
        .HasForeignKey(ur => ur.KeywordId)
        .IsRequired();
});

You should try to create new CourseKeyword instance by keys not by entities.您应该尝试通过键而不是实体来创建新的 CourseKeyword 实例。 I mean:我是说:

var key = new CourseKeyword()
{
    CourseId = course.Id,
    KeywordId = keyw.Id
};

Try this code:试试这个代码:

var key = new CourseKeyword()
{
    CourseId = course.Id,
    KeywordId = keyw.Id
};

_db.CourseKeywords.Add(key);
_db.SaveChanges();

and by the way fix your you dbContext:顺便修复你的dbContext:

builder.Entity<CourseKeyword>(courseKeyword =>
{
    courseKeyword.HasKey(ur => new { ur.KeywordId, ur.CourseId });

    courseKeyword.HasOne(ur => ur.Course)
        .WithMany(r => r.Keywords)
        .HasForeignKey(ur => ur.CourseId)
       .OnDelete(DeleteBehavior.ClientSetNull);

    courseKeyword.HasOne(ur => ur.Keyword)
        .WithMany(r => r.CourseKeywords)
        .HasForeignKey(ur => ur.KeywordId)
        .OnDelete(DeleteBehavior.ClientSetNull);

});

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

相关问题 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 SqlException:INSERT 语句与 FOREIGN KEY 约束冲突 - asp.net-core - SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint - asp.net-core ASP.NET 核心 Web API - 如何解决 INSERT 语句与 FOREIGN KEY 约束冲突 - ASP.NET Core Web API - How to resolve The INSERT statement conflicted with the FOREIGN KEY constraint Asp.net 核心标识“INSERT 语句与 FOREIGN KEY 约束冲突” - Asp.net core Identity "The INSERT statement conflicted with the FOREIGN KEY constraint " INSERT 语句与 FOREIGN KEY 约束冲突 - asp.net.mvc5 - The INSERT statement conflicted with the FOREIGN KEY constraint - asp.net.mvc5 ASP.NET插入语句与外键约束冲突 - ASP.NET the insert statement conflicted with the foreign key constraint INSERT语句与FOREIGN KEY约束冲突 - The INSERT statement conflicted with the FOREIGN KEY constraint BulkInsert:INSERT语句与FOREIGN KEY约束冲突 - BulkInsert: The INSERT statement conflicted with the FOREIGN KEY constraint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM