I'm trying to solve this problem for hours. I believe it's some kind of relationship problem.
I get this error:
The DELETE statement conflicted with the REFERENCE constraint "FK_NoteTag _Notes_NoteId". The conflict occurred in database "db", table "dbo.NoteTag", column 'NoteId'.
These are my model classes:
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, int tagId, 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; }
}
And here is the DB context:
//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
);
And here is the seed of DB:
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", Admin = false
},
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, 0),
GroupId = 1
},
new
{
NoteId = 2,
NoteName = "Naučit se programova",
NoteText = "- sjet C# tutorial",
NoteDuration = DateTime.Now,
NoteCreation = new DateTime(2020, 3, 1, 7, 0, 0),
GroupId = 2
},
new
{
NoteId = 3,
NoteName = "B7",
NoteText = "Vyjít lysou",
NoteDuration = DateTime.Now,
NoteCreation = new DateTime(2018, 3, 1, 7, 0, 0),
GroupId = 3
},
new
{
NoteId = 4,
NoteName = "B12",
NoteText = "Udělat salto",
NoteDuration = DateTime.Now,
NoteCreation = new DateTime(2019, 3, 1, 7, 0, 0),
GroupId = 4
}
};
object[] tags =
{
new {TagId = 1, TagName = "Vaření", UserId = 1},
new {TagId = 2, TagName = "Sport", UserId = 2},
new {TagId = 3, TagName = "Sebe vyvoj", UserId = 3},
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 = 1},
new {GroupId = 2, TagId = 2},
new {GroupId = 3, TagId = 3},
new {GroupId = 4, TagId = 4}
};
Thank you for your help