Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

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

$
0
0

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

one set of code

publicclassUser{publicintUserId{get;set;}publicstringUsername{get;set;}publicstringPassword{get;set;}publicvirtualICollection<Role>Roles{get;set;}}publicclassRole{publicintRoleId{get;set;}publicstringRolename{get;set;}publicvirtualICollection<User>Users{get;set;}}classConfigurationContext:DbContext{publicConfigurationContext(){Database.SetInitializer<ConfigurationContext>(newDropCreateDatabaseIfModelChanges<ConfigurationContext>());}publicDbSet<Role>Roles{get;set;}publicDbSet<User>Users{get;set;}}

second set of code

publicclassUser{publicintUserId{get;set;}publicstringUsername{get;set;}publicstringPassword{get;set;}publicvirtualICollection<Role>Roles{get;set;}}publicclassRole{publicintRoleId{get;set;}publicstringRolename{get;set;}}classConfigurationContext:DbContext{publicConfigurationContext(){Database.SetInitializer<ConfigurationContext>(newDropCreateDatabaseIfModelChanges<ConfigurationContext>());}protectedoverridevoidOnModelCreating(DbModelBuilder modelBuilder){
        modelBuilder.Entity<User>().HasMany(u => u.Roles).WithMany().Map(m =>{
                m.ToTable("UserRoles");
                m.MapLeftKey("UserId");
                m.MapRightKey("RoleId");});}publicDbSet<Role>Roles{get;set;}publicDbSet<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.


Viewing all articles
Browse latest Browse all 1698

Trending Articles