Base solution for your next web application
Open Closed

Many-to-Many #1433


User avatar
0
andry3ag created

Hi,

how do I establish many-to-many relationship? For example, if a blog has many entries and and entry can belong to many blogs?

I tried setting doing it code first as suggested in this url <a class="postlink" href="http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx">http://www.entityframeworktutorial.net/ ... first.aspx</a> but when I do Add-Migration, the script do not seems to create the joining table.

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

4 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Can you share your Blog and Entry entities ?

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    easyest created

    Entity framework does not create the table because primary keys in both entities have same name "Id". For entity framework to generate correct relationship, primary key names should be different. Your only solution is to use Fluent API.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    andry3ag created

    I see, I will use fluent api then. Thanks alot guys.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    easyest created

    By the way, I do have another problem with many-to-many relationship - after adding navigational property, I get an error

    The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.
    

    But this can be due to my heavily spoiled testing project and I intend to check it on a fresh project when I will test last version.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)