Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

c# - entity framework - many to many relationship

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...