2

The context is the following:

A person may belong to a group (he would be a member) and may be admin of a group. This is modeled like this in code-first:

class Group
{
     [InverseProperty("GroupsWhereIamAdmin")]
     public virtual ICollection<Person> Admins {get; set;}
     [InverseProperty("GroupsWhereIamMember")]
     public virtual ICollection<Person> Members {get; set;}
}

class Person
{
     [InverseProperty("Members")]
     public virtual ICollection<Group> GroupsWhereIamMember {get; set;}
     [InverseProperty("Admins")]
     public virtual ICollection<Group> GroupsWhereIamAdmin {get; set;}
}

The problem is that the name of the generated tables are these:

GroupPersons
GroupPersons1

And I would like them to be:

GroupAdmins
GroupMembers

How can I achieve this in a simple way? (ie: using attributes)

sports
  • 7,851
  • 14
  • 72
  • 129

1 Answers1

3

So I guess this has to be done with Fluent API, so I removed the data annotations, ie: [InverseProperty(...)]'s, and did this:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // Many to many: A group has members, and a person has groups.
        modelBuilder.Entity<Group>()
            .HasMany(e => e.Members)
            .WithMany(m => m.GroupsWhereIAmMember)
            .Map(mc =>
            {
                mc.ToTable("GroupMembers");
                mc.MapLeftKey("GroupId");
                mc.MapRightKey("MemberId");
            });

        // Many to many: A group has admins, and a person has groups where he is admin
        modelBuilder.Entity<Group>()
            .HasMany(e => e.Admins)
            .WithMany(m => m.GroupsWhereIAmAdmin)
            .Map(mc =>
            {
                mc.ToTable("GroupAdmins");
                mc.MapLeftKey("GroupId");
                mc.MapRightKey("AdminId");
            });
    }
sports
  • 7,851
  • 14
  • 72
  • 129