3

Hi I try use Many to Many relationship with EF Fluent API. I have 2 POCO classes.

public class Project
{
    public int ProjectId { get; set; }

    public virtual ICollection<Author> Authors { get; set; }

    public Project()
    {
        Authors = new List<Author>();
    }
}

public class Author
{
    public int AuthorId { get; set; }

    public virtual ICollection<Project> Projects { get; set; }

    public Author()
    {
        Projects = new List<Project>();
    }
}

And I map many to many relationship with this part of code:

        ////MANY TO MANY 
        modelBuilder.Entity<Project>()
            .HasMany<Author>(a => a.Authors)
            .WithMany(p => p.Projects)
            .Map(m =>
                     {
                         m.ToTable("ProjectAuthors");
                         m.MapLeftKey("ProjectId");
                         m.MapRightKey("AuthorId");
                     });

This created table ProjectsAuthors in DB. It is my first attempt with this case of relationship mapping.

If I omitted this mapping it created table AuthorProject with similar schema. It is correct bevahior?

Askolein
  • 3,250
  • 3
  • 28
  • 40
  • @Ladislav: Nothing. I asked if this is common EF behavior when I am creating many to many relationship. You are Slovak or Cezch ? –  Oct 29 '11 at 08:15
  • Yes I'm Czech. As I understand your question the behaviour is correct. – Ladislav Mrnka Oct 29 '11 at 09:28
  • @Ladislav: I read a few articles about EF on your blog, good job :) –  Oct 31 '11 at 10:39

1 Answers1

6

By trial and error I found the following. Given two classes...

public class AClass
{
    public int Id { get; set; }
    public ICollection<BClass> BClasses { get; set; }
}

public class BClass
{
    public int Id { get; set; }
    public ICollection<AClass> AClasses { get; set; }
}

...and no Fluent mapping and a DbContext like this...

public class MyContext : DbContext
{
    public DbSet<AClass> AClasses { get; set; }
    public DbSet<BClass> BClasses { get; set; }
}

...the name of the created join table is BClassAClasses. If I change the order of the sets...

public class MyContext : DbContext
{
    public DbSet<BClass> BClasses { get; set; }
    public DbSet<AClass> AClasses { get; set; }
}

...the name of the created join table changes to AClassBClasses and the order of the key columns in the table changes as well. So, the name of the join table and the order of the key columns seems to depend on the order in which the entity classes are "loaded" into the model - which can be the order of the DbSet declarations or another order if more relationship are involved - for example some other entity refering to AClass.

In the end, it doesn't matter at all, because such a many-to-many relationship is "symmetric". If you want to have your own name of the join table, you can specify it in Fluent API as you already did.

So, to your question: Yes, naming the join table AuthorProjects is correct behaviour. If the name had been ProjectAuthors it would be correct behaviour as well though.

Slauma
  • 175,098
  • 59
  • 401
  • 420