ASP.NET Core-DELETE语句与REFERENCE约束冲突数据库中发生了冲突

问题描述

我试图解决这个问题几个小时。我相信这是某种关系问题。

我收到此错误

DELETE语句与REFERENCE约束“ FK_NoteTag”冲突 _Notes_NoteId”。在数据库“ db”,表“ dbo.NoteTag”的“ NoteId”列中发生了冲突。

这些是我的模型类:

public class Group
{
    public Group()
    { }
    
    public Group(int groupId,string groupName)
    {
        GroupId = groupId;
        GroupName = groupName;
    }
    
    [Key] 
    public int? GroupId { get; set; }
    [required] 
    public string GroupName { get; set; }

    public virtual User User { get; set; }
    public virtual List<Note> Notes { get; set; }
    public virtual IList<GroupTag> GroupTag { get; set; }
}

public class GroupTag
{
    public GroupTag()
    { }

    public GroupTag(int groupId,Group group,int tagId,Tag tag)
    {
        GroupId = groupId;
        Group = group;
        TagId = tagId;
        Tag = tag;
    }
            
    public virtual int? GroupId { get; set; }
    public virtual Group Group { get; set; }
    public virtual int? TagId { get; set; }
    public virtual Tag Tag { get; set; }
}

public class Note
{
    public Note()
    { }

    public Note(int noteId,string noteName,string noteText,DateTime noteDuration,DateTime noteCreation)
    {
        NoteId = noteId;
        NoteName = noteName;
        NoteText = noteText;
        NoteDuration = noteDuration;
        NoteCreation = noteCreation;
    }

    [Key] 
    public int? NoteId { get; set; }
    [required] 
    public string NoteName { get; set; }

    public string NoteText { get; set; }
    public DateTime NoteDuration { get; set; }
    public DateTime NoteCreation { get; set; }

    public virtual Group Group { get; set; }
    public virtual IList<NoteTag> NoteTag { get; set; }
}

public class NoteTag
{
    public NoteTag()
    { }

    public NoteTag(int noteId,Note note,Tag tag)
    {
        NoteId = noteId;
        Note = note;
        TagId = tagId;
        Tag = tag;
    }

    public virtual int? NoteId { get; set; }
    public virtual Note Note { get; set; }
    public virtual int? TagId { get; set; }
    public virtual Tag Tag { get; set; }
}

public class Tag
{
    public Tag()
    { }

    public Tag(int tagId,string tagName)
    {
        TagId = tagId;
        TagName = tagName;
    }

    [Key] 
    public int? TagId { get; set; }
    [required] 
    public string TagName { get; set; }

    // owner
    public virtual User User { get; set; }

    public virtual IList<NoteTag> NoteTag { get; set; }
    public virtual IList<GroupTag> GroupTag { get; set; }
}

public class User
{
    public User()
    { }

    public User(int userId,string nickname,string password,bool admin)
    {
        UserId = userId;
        Nickname = nickname;
        Password = password;
        Admin = admin;
    }

    [Key] 
    public int? UserId { get; set; }
    [required] 
    public string Nickname { get; set; }
    [required] 
    public string Password { get; set; }
    [required] 
    public bool Admin { get; set; }

    public virtual List<Tag> Tags { get; set; }
    public virtual IList<Group>  Groups { get; set; }
}

这是数据库上下文:

 //M:M keys (1/3) NoteTag
            modelBuilder.Entity<NoteTag>().HasKey(nt => new {nt.NoteId,nt.TagId});

            //M:M keys (2/3)
            modelBuilder.Entity<NoteTag>()
                .HasOne<Note>(nt => nt.Note)
                .WithMany(t => t.NoteTag)
                .OnDelete(DeleteBehavior.ClientSetNull);

            //M:M keys (3/3)
            modelBuilder.Entity<NoteTag>()
                .HasOne<Tag>(nt => nt.Tag)
                .WithMany(n => n.NoteTag)
                .OnDelete(DeleteBehavior.ClientSetNull);
            //
            //M:M keys (1/3) GroupTag
            modelBuilder.Entity<GroupTag>().HasKey(gt => new {gt.GroupId,gt.TagId});

            //M:M (2/3)
            modelBuilder.Entity<GroupTag>()
                .HasOne<Group>(gt => gt.Group)
                .WithMany(t => t.GroupTag)
                .OnDelete(DeleteBehavior.ClientSetNull);

            //M:M (3/3)
            modelBuilder.Entity<GroupTag>()
                .HasOne<Tag>(gt => gt.Tag)
                .WithMany(g => g.GroupTag)
                .OnDelete(DeleteBehavior.ClientSetNull);
            
            modelBuilder.Entity<Group>()
                .HasMany(g => g.Notes)
                .WithOne(u => u.Group)
                .OnDelete(DeleteBehavior.Cascade);
            
            modelBuilder.Entity<User>()
                .HasMany(u => u.Groups)
                .WithOne(g => g.User)
                .OnDelete(DeleteBehavior.Cascade);
            
            modelBuilder.Entity<User>()
                .HasMany(u => u.Tags)
                .WithOne(g => g.User)
                .OnDelete(DeleteBehavior.Cascade);
            
            
            modelBuilder.Entity<Note>()
                .HasData(
                    notes
                );

            modelBuilder.Entity<Tag>()
                .HasData(
                    tags
                );

            modelBuilder.Entity<User>()
                .HasData(
                    users
                );

            modelBuilder.Entity<Group>()
                .HasData(
                    groups
                );

            modelBuilder.Entity<NoteTag>()
                .HasData(
                    noteTag
                );

            modelBuilder.Entity<GroupTag>()
                .HasData(
                    groupTag
                );

这是数据库的种子:

object[] users =
            {
                new
                {
                    UserId = 1,Nickname = "AwesomeUser",Password = "nicepass",Admin = true
                },new
                {
                    UserId = 2,Nickname = "LameUser",Password = "nopass",Admin = false
                },new
                {
                    UserId = 3,Nickname = "scooby",Password = "pass",new
                {
                    UserId = 4,Nickname = "doo",Password = "hezogeg",Admin = false
                }
            };

            object[] groups =
            {
                new {GroupId = 1,GroupName = "leden",UserId = 1},new {GroupId = 2,GroupName = "unor",UserId = 2},new {GroupId = 3,GroupName = "brezen",UserId = 3},new {GroupId = 4,GroupName = "duben",UserId = 4}
            };

            object[] notes =
            {
                new
                {
                    NoteId = 1,NoteName = "Uvařit oběd",NoteText = "1. Brambory 2. maso",NoteDuration = DateTime.Now,NoteCreation = new DateTime(2008,3,1,7,0),GroupId = 1
                },new
                {
                    NoteId = 2,NoteName = "Naučit se programova",NoteText = "- sjet C# tutorial",NoteCreation = new DateTime(2020,GroupId = 2
                },new
                {
                    NoteId = 3,NoteName = "B7",NoteText = "Vyjít lysou",NoteCreation = new DateTime(2018,GroupId = 3
                },new
                {
                    NoteId = 4,NoteName = "B12",NoteText = "Udělat salto",NoteCreation = new DateTime(2019,GroupId = 4
                }
            };

            object[] tags =
            {
                new {TagId = 1,TagName = "Vaření",new {TagId = 2,TagName = "Sport",new {TagId = 3,TagName = "Sebe vyvoj",new {TagId = 4,TagName = "Gymnastika",UserId = 4}
            };

            object[] noteTag =
            {
                new {NoteId = 1,TagId = 1},new {NoteId = 2,TagId = 2},new {NoteId = 3,TagId = 3},new {NoteId = 4,TagId = 4}
            };

            object[] groupTag =
            {
                new {GroupId = 1,TagId = 4}
            };

感谢您的帮助

解决方法

最后我发现了问题...我需要外键规范。我以为流利的api自然解决了。谢谢你们