0

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!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user467566
  • 155
  • 1
  • 4
  • 13
  • 1
    So your library can't own any self titled auto-biographies? – Jeremy Lakeman Nov 20 '22 at 23:52
  • I want the name of the book not to be equal to the name of the author. And so that it is processed in the database too – user467566 Nov 20 '22 at 23:55
  • I think this is a bad idea. Could you explain why you want to do it? – tymtam Nov 21 '22 at 00:01
  • @tymtam, I want it to be like this. I'm creating a book with a specific title and you can add authors to it. I want that when adding an author with the same title as the book, an error will appear that "the author's title cannot be the same as the book." How to do it in ASP.NET Core, I know. But I don't know how to make it work in DB. And I would like to do it with Entity Framework. Set configuration – user467566 Nov 21 '22 at 00:05

1 Answers1

1

Your Author class can implement the IValidatableObject interface.

This way, you will have a Validate method like this:

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
    if (Author.Name == Book.Name)
    {
        yield return new ValidationResult(
            $"Author name and Book name can't have the same value.",
            new[] { nameof(Name) });
    }
}

Then, you can manage the ModelState validation in your controller action as something like:

if (!ModelState.IsValid)
{
    // Do something when validation is not ok
}

To prevent this to be commited on database side, it would be better to ensure the data consitency on database side by implementing a check constraint directly in SQL on your Author table.

Gambi
  • 464
  • 4
  • 12
  • Thanks. "To prevent this to be commited on database side, it would be better to ensure the data consitency on database side by implementing a check constraint directly in SQL on your Author table" - can I do it via Entity Framework? – user467566 Nov 20 '22 at 23:53
  • 1
    It seems that you can implement HasCheckConstraint with the model builder : https://stackoverflow.com/a/60199728/14766188. But I think your check is too complex for EF Core, that's why you should do it on the database side. – Gambi Nov 20 '22 at 23:57
  • `Author.Name == Book.Name` is very weak. It's easy to "hack" it with extra spaces, capital/common casing. etc. – tymtam Nov 21 '22 at 00:00
  • @Gambi Will it work for the author who owns the book? – user467566 Nov 21 '22 at 00:02
  • @tymtam, what do you suggest? – user467566 Nov 21 '22 at 00:03