I'm creating an application using ASP.NET Core and Entity Framework. I have two models:
class Book
{
public int Id { get; set; }
public string Name { get; set; }
public List<Author> Authors { get; set; }
}
class Author
{
public int Id { get; set; }
public string Name { get; set; }
public Book Book { get; set; }
}
I want that when creating an author, his name cannot be created the same as the book. I want to add validation to Author.Name (Something like this: Author.Name != Book.Name
.
Also, I want this rule to enter into the db too. So I think it possible to make in DbContext
(OnModelCreating
).
How can I do it? Thanks a lot!