2

I have a strange relation in my database.

Basically it's an entity connected to entities of the same type.

What I have is this:

Model:

public class Article
{
    -- Properties --

    ICollection<Article> ConnectedArticles { get; set; }
} 

Fluent API:

modelBuilder.Entity<Article>()
    .HasMany(ca => ca.ConnectedArticles)
    .WithMany(ca => ca.ConnectedArticles)
    .Map(m => 
    { 
        m.MapLeftKey("ArtNo"); 
        m.MapRightKey("ArtNo");
        m.ToTable("ConnectedArticles");
    });

This does not play well with Entity Framework and results in the error: The navigation property 'ConnectedArticles' declared on type 'ServiceSystem.Models.CommonArticle' cannot be the inverse of itself.

I could solve this issue by creating a new model which contains the related articles like so:

public class ConnectedArticle
{
    public Article article1 { get; set; }
    public Article article2 { get; set; }
}

But I'm hoping Entity Framework can do this on it's own, just like it does with many-to-many for separate entities.

Is it possible to solve this nicely with Entity Framework or am I missing something critical?

Thanks, Robin Dorbell

Robin Dorbell
  • 1,569
  • 1
  • 13
  • 26
  • I would take a look at the fact that both your `MapLeftKey` and `MapRightKey` are the same column. You'll probably need to have a different column to be able to distinguish how they relate to each other that way. – krillgar Aug 13 '14 at 12:32
  • Thanks for the input, but the relationship is not different from one another, they're just related. – Robin Dorbell Aug 13 '14 at 12:38
  • Could http://stackoverflow.com/questions/12237617/entityframework-same-table-many-to-many-relationship help? – Jontatas Aug 13 '14 at 12:55
  • hmmm what a coincidence, similar type of question I just answered [here](http://stackoverflow.com/questions/25272175/many-to-many-collection-of-same-entity-with-two-way-relationship) – Yuliam Chandra Aug 13 '14 at 13:12

0 Answers0