EF Code first - many to many relation mapping table with extra columns

ef-code-first ef-migrations entity-framework entity-framework-6

Question

I have both a group model and a user model. There is a many-to-many link between User and Group. I want to have a mapping table when I convert this to a table. To accomplish this, I'm using the subsequent.

modelBuilder.Entity<UserGroup>()
        .HasMany(a => a.Users)
         .WithMany(b => b.UserGroup)

         .Map(mc =>
         {

             mc.ToTable("UserUserGroupMapping");
             mc.MapLeftKey("UserId");
             mc.MapRightKey("UserGroupId");

         });

By doing this, a table with the fields UserId and UserGroupId is created. However, I face some difficulties.

To this table, I would like to be able to add an Identity column as well as some audit fields (such Created by and Created Date). I'm unsure on how to proceed.

Can somebody help me with this?

Thanks

1
4
6/24/2015 4:49:59 PM

Accepted Answer

If you take the following actions, it should work:

  1. Remove the configuration you displayed in the aforementioned code snippet.
  2. Create a mapping table and set its table name to correspond to the name of the original table.

    // name this whatever you want
    class UserUserGroupMapping
    {
        public UserUserGroupMappingId { get; set; }
        public int UserId { get; set; }
        public virtual User User { get; set; } 
        public int UserGroupId { get; set; }
        public virtual UserGroup UserGroup { get; set; } 
        // other properties
    }
    

    modelBuilder.Entity<UserUserGroupMapping>()
        .HasKey(um => um.UserUserGroupMappingId)
        .ToTable("UserUserGroupMapping");
    
  3. Change the attributes of the many-to-many collection fromUser and UserGroup and substitute one-to-many relationships instead

    class User
    {
        // other properties
        // remove this:
        // public virtual ICollection<UserGroup> UserGroup { get; set; }
        public virtual ICollection<UserUserGroupMapping> UserGroupMappings { get; set; }
    }
    
    class UserGroup
    {
        // other properties
        // remove this:
        // public virtual ICollection<User> Users { get; set; }
        public virtual ICollection<UserUserGroupMapping> UserMappings { get; set; }
    }
    

    modelBuilder.Entity<UserUserGroupMapping>()
        .HasRequired(um => um.UserGroup).WithMany(g => g.UserMappings)
        .HasForeignKey(um => um.UserGroupId);
    
    modelBuilder.Entity<UserUserGroupMapping>()
        .HasRequired(um => um.User).WithMany(g => g.UserGroupMappings)
        .HasForeignKey(um => um.UserId);
    
  4. Put the package manager to use toAdd-Migration ... eliminate any code from the scaffolded migration that could try to dump the old table and construct a new one. The migration must at the very least (and I may be missing some):

    • DropPrimaryKey regarding the initial important columns
    • AddColumn with regard to the new columnsInt(identity:true, nullable: false) pertaining to the new main key column)
    • AddPrimaryKey because of the new key column

Then, you can retrieve entities using the techniques described in this response.

8
5/23/2017 11:33:13 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow