This is just an example so you can understand, this is a system with people and their favorite books.
This is what the models look like:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public List<Book> favBooks { get; set; }
}
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public int PageCount { get; set; }
}
My problem is if I add a book and 2 people and both have this as a FAV book, then one person is overwritten.
Book mathForDummies = new Book()
{
Name = "Math for Dummies",
PageCount = 457
};
_db.book.Add(mathForDummies);
_db.SaveChanges();
Person lena = new Person()
{
Name = "Lena",
favBooks = new List<Book>
{
mathForDummies
}
};
Person max = new Person()
{
Name = "Max",
favBooks = new List<Book>
{
mathForDummies
}
};
_db.person.Add(lena);
_db.person.Add(max);
_db.SaveChanges();
In this case, both have this book, but max overwrites lena.
Is there a better way, or am I overlooking something?

