none
How many to many relation can be established without 3rd table in EF RRS feed

  • Question

  • just i saw this post https://stackoverflow.com/a/16367951

    one set of code

    public class User
    {
        public int UserId { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
    
        public virtual ICollection<Role> Roles { get; set; }
    }
    
    public class Role
    {
        public int RoleId { get; set; }
        public string Rolename { get; set; }
    
        public virtual ICollection<User> Users { get; set; }
    }
    
    class ConfigurationContext : DbContext
    {
        public ConfigurationContext()
        {
            Database.SetInitializer<ConfigurationContext>(
                new DropCreateDatabaseIfModelChanges<ConfigurationContext>());
        }
    
        public DbSet<Role> Roles { get; set; }
        public DbSet<User> Users { get; set; }
    }

    second set of code

    public class User
    {
        public int UserId { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
    
        public virtual ICollection<Role> Roles { get; set; }
    }
    
    public class Role
    {
        public int RoleId { get; set; }
        public string Rolename { get; set; }
    }
    
    class ConfigurationContext : DbContext
    {
        public ConfigurationContext()
        {
            Database.SetInitializer<ConfigurationContext>(
                new DropCreateDatabaseIfModelChanges<ConfigurationContext>());
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>()
                .HasMany(u => u.Roles)
                .WithMany()
                .Map(m => {
                    m.ToTable("UserRoles");
                    m.MapLeftKey("UserId");
                    m.MapRightKey("RoleId");
                });
        }
    
        public DbSet<Role> Roles { get; set; }
        public DbSet<User> Users { get; set; }
    }

    see the 2nd set of code and tell me how EF manage to create many to many relation can be established without 3rd table.

    please guide me. are the EF internally create any 3rd table which is not visible to user.

    Monday, July 17, 2017 12:20 PM

All replies

  • Hi Mou_kolkata,

    Thank you for posting the problem in ADO.NET Entity Framework and LINQ to Entities.

    >>see the 2nd set of code and tell me how EF manage to create many to many relation can be established without 3rd table.

    I don’t suppose that it is very accurate. For the 2nd set of code, it also created 3<sup>rd</sup> table by the following code.

    modelBuilder.Entity<User>()
    
                .HasMany(u => u.Roles)
    
                .WithMany()
    
                .Map(m => {
    
                    m.ToTable("UserRoles");// the code creates a table for managing the User table and Role table relationship
    
                    m.MapLeftKey("UserId");
    
                    m.MapRightKey("RoleId");
    
                });
    

    The 2<sup>nd</sup> set of code doesn’t only create 3<sup>rd</sup> model. The Fluent API configure a Many-to-Many relationship between User and Role.

    If you would like to establish many to many relationship and the following link will give you some explanation, please refer to following link.

    http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

    If you have any other question for my reply, please feel free to contact me.

    Best Regards,

    Aaron.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 19, 2017 2:20 AM
    Moderator