8

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).

Community
  • 1
  • 1
Marthijn
  • 3,292
  • 2
  • 31
  • 48

2 Answers2

18

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
}
Slauma
  • 175,098
  • 59
  • 401
  • 420
  • Thanks for your answer! However, for both solutions I get the following exception: `A first chance exception of type 'System.InvalidOperationException' occurred in EntityFramework.DLL` – Marthijn Nov 23 '11 at 07:38
  • Correction: the fluent solution works (my mistake..), so thanks for that! Any idea why the `InverseProperty` throws an exception? – Marthijn Nov 23 '11 at 07:49
  • @Henkie: Does the application really crash? "First chance exception" is usually not a problem and only shown in debugger output window. Normally it can be ignored. – Slauma Nov 23 '11 at 09:22
  • The application doesn't crash indeed, but the database scheme isn't generated. I'm using VS Express so I can't break on first chance exceptions. – Marthijn Nov 23 '11 at 10:36
  • The [InverseProperty] tag worked like a charm for me, many thanks! – L4marr Feb 09 '22 at 14:35
0

If table B has foreign key to table A then class B has navigation property to A and A have navigation property to ICollection<A>.
If table B has many to many relation with table A then class A must have ICollection<B> and class B must have ICollection<A>.

Try that, maybe this will clarify your request from the EF.

Naor
  • 23,465
  • 48
  • 152
  • 268
  • I forgot to add the ICollection to `class A` for the many-to-many association in my code example. It's fixed now. – Marthijn Nov 22 '11 at 15:26