0
\$\begingroup\$

Let's say you have a book. A book can have an author, an editor, a proof reader, or several other credits. Which credits are available should be dynamic (someone might not want to have the "proof reader" credit, or want to add something not available "out of the box"). Each credit can have one or many people.

I would normally model it like this in the database

Book        Credit        Person
----        ------        ------
ID          ID            ID
Title       Name          Name


BookCreditPersonRelation
--------------------------
FK_BookID
FK_CreditID
FK_PersonID

And for code-first EFCore I would write it like this:

public class Book {
    public int ID { get; set; }
    public string Title { get; set; }
    public ICollection<BookCreditPersonRelation> BookCreditPersonRelations { get; set; }
}
public class Credit {
    public int ID { get; set; }
    public string Name { get; set; }
    public ICollection<BookCreditPersonRelation> BookCreditPersonRelations { get; set; }
}
public class Person {
    public int ID { get; set; }
    public string Name { get; set; }
    public ICollection<BookCreditPersonRelation> BookCreditPersonRelations { get; set; }
}
public class BookCreditPersonRelation {
    public Book Book { get; set;
    public Credit Credit { get; set; }
    public Person { get; set; }
}

Does this look "right" to you? Is there a "Better Way" (TM)

\$\endgroup\$
1

1 Answer 1

1
\$\begingroup\$

With your given model, the BookCreditPersonRelations found on Book would have multiple entries for the same credit, when you have multiple people for that credit. IMO, it's not how you'd give credits on a book. Generally, you have a clear relation as, Book -> Credit -> Person, which is better represented below.

How I'd structure my db model is:

Book        Credit        Person
----        ------        ------
ID          ID            ID
Title       Name          Name
            FK_BookID

CreditPersonRelation
--------------------
FK_CreditID
FK_PersonID

Then my code-first EFCore model would look like:

public class Book {
  public int ID { get; set; }
  public string Title { get; set; }
  public ICollection<Credit> Credits { get; set; }
}

public class Credit {
  public int ID { get; set; }
  public string Name { get; set; }
  public Book Book { get; set; }
  public ICollection<Person> Persons { get; set; }
}

public class Person {
  public int ID { get; set; }
  public string Name { get; set; }
  public ICollection<Credit> Credits { get; set; }
}

public class CreditPersonRelation {
  public Credit Credit { get; set; }
  public Person Person { get; set; }
}

IMO, this makes a better composition of the entities.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.