Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6856065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:50:15+00:00 2026-05-27T01:50:15+00:00

I have a many-to-many association in EF Code-First (as explained in this question), and

  • 0

I have a many-to-many association in EF Code-First (as explained in this question), and I want to use a one-to-many to the same entity as well. The problem is EF does not produce the right database scheme. Code:

public class A
{
  public int Id { get; set; }
  public string Name { get; set; }
  public virtual ICollection<B> ObjectsOfB { get; set; }
}

public class B
{
  public int Id { get; set; }
  public virtual A ObjectA { get; set; }
  public virtual ICollection<A> OtherObjectsOfA { get; set; }
}

When I remove the ObjectA property of class B the many-to-many association is generated correctly.
When generated incorrectly, entity B gets 2 foreign keys to A, and entity A gets 1 foreign key to B (like a many-to-one relation).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:50:16+00:00Added an answer on May 27, 2026 at 1:50 am

    If you have more than one navigation property refering to the same entity EF does not know where the inverse navigation property on the other entity belongs to. In your example: Does A.ObjectsOfB refer to B.ObjectA or to B.OtherObjectsOfA? Both would be possible and a valid model.

    Now, EF does not throw an exception like “cannot determine relationships unambiguously” or something. Instead it decides that B.ObjectA refers to a third endpoint in B which is not exposed as navigation property in the model. This creates the first foreign key in table B. The two navigation properties in B refer to two endpoints in A which are also not exposed in the model: B.ObjectA creats the second foreign key in table B and B.OtherObjectsOfA creates a foreign key in table A.

    To fix this you must specify the relationships explicitely.

    Option one (the easiest way) is to use the InverseProperty attribute:

    public class A
    {
        public int Id { get; set; }
        public string Name { get; set; }
        [InverseProperty("OtherObjectsOfA")]
        public virtual ICollection<B> ObjectsOfB { get; set; }
    }
    

    This defines that A.ObjectsOfB is part of a many-to-many relation to B.OtherObjectsOfA.

    The other option is to define the relationships completely in Fluent API:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<A>()
            .HasMany(a => a.ObjectsOfB)
            .WithMany(b => b.OtherObjectsOfA)
            .Map(x =>
            {
                x.MapLeftKey("AId");
                x.MapRightKey("BId");
                x.ToTable("ABs");
            });
    
        modelBuilder.Entity<B>()
            .HasRequired(b => b.ObjectA)  // or HasOptional
            .WithMany()
            .WillCascadeOnDelete(false);  // not sure if necessary, you can try it
                                          // without if you want cascading delete
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my issue lokks similar to this one: (link) but i have one-to-many association: <set
I have a Product and Category entity in many-to-many association. I am trying to
I'm struggling with creating a simple one-to-many relationship using Code First in EF. I
I am loading a list of many entities. These entities have a one-to-many association
I have just transferred my EF 4.0 Solution to use EF 4.1 Code-First. Everything
I have Product model and it has many categories with a has_many :through association
I have the following one to many associations. Document has many Sections and Section
Currently I have database with the following associations: One Client to Many Intakes One
I have many items inside a list control. I want each item to have
I have many small files containing code fragments, pseudo-code algorithms, classes, templates, SQL-samples, etc.,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.