Many to many EF core - Unable to determine the relationship

c# entity-framework entity-framework-core many-to-many

Question

I'm trying to solve a many to many relationship in EF core, but I keep getting an exception (Unable to determine the raltionship represented by navigation property 'Category.contacts' of type List)

I'm learning EF core and I've read a lot of posts about this issue already but I cannot solve it on my own. I believe it will be asked on the exam.

I've made a class to solve the many to many problem, but how do I configure this correctly using the fluent api?

This is my code:

public class Contact{
    public int PersonId {get; set;}
    public List<Category> Categories {get; set;}
}

public class Category{
    public int CategoryId {get; set;}
    public List<Category> Categories {get; set;}
}

public class ContactCategory{
    public int PersonId {get; set;}
    public int CategoryId {get; set;}
    public Contact Contact {get; set;}
    public Category Category {get; set;}
}

//Inside the DbContext class:
protected override void OnModelCreating(ModelBuilder modelBuilder){
    modelBuilder.Entity<ContactCategory>().HasKey(x => new {x.PersonId, x.CategoryId});
}

The exception itself:

Unhandled Exception: System.TypeInitializationException: The type initializer for 'Contacts.UI.CA.Program' threw an exception. ---> System.InvalidOperationException: Unable to determine the relationship represented by navigation property 'Category.Contacts' of type 'List'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

1
1
8/10/2019 10:13:08 AM

Accepted Answer

The many-to-many relationship is a tricky one; we need to understand how these kinds of relationships are built. Usually, we will build two different entities that require a many-to-many relationship, create an entity that will be purely used to join the first two entities, and then map one-to-many between this entity (created to join two separate one-to-many relationships) and the two entities (created first) separately:

public class Contact
{
    public int Id {get; set;}
    public ICollection<ContactCategory> ContactCategories {get; set;}
}

public class Category
{
    public int Id { get; set; }
    public ICollection<ContactCategory> ContactCategories { get; set; }
}

public class ContactCategory
{
    public int Id { get; set; }
    public int ContactId {get; set;}
    public Contact Contact {get; set;}
    public int CategoryId {get; set;}
    public Category Category {get; set;}
}

The Contact and Category entities require many-to-many relationships for which the ContactCategory entity is created, and its only job is to join the Contact and Category entities.

We should configure the relationship in the Fluent API in two different one-to-many relationships between the Contact and ContactCategory and Category and ContactCategory entities. The HasForeignKey method doesn't need to be generic, since one-to-many relationships don't need to mark the dependent type explicitly:

The many-to-many relationship between Contact and Category entities require the following configuration:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  modelBuilder.Entity<ContactCategory>()
    .HasOne(x => x.Category)
    .WithMany(x => x.ContactCategories)
    .HasForeignKey(x => x.CategoryId);

  modelBuilder.Entity<ContactCategory>()
    .HasOne(x => x.Contact)
    .WithMany(x => x.ContactCategories)
    .HasForeignKey(x => x.ContactId);
}

Good luck!

1
8/10/2019 5:17:02 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow