EF 4.2 Code First. How to name the many-to-many table?

Can a table name be specified in a many-to-many relationship?

Example:

class A { public int Id { get; set; } // ..... public virtual ICollection<B> BCollection { get; set; } } class B { public int Id { get; set; } // ..... public virtual ICollection<A> ACollection { get; set; } } 

I thought this might work, but it doesn't seem to be.

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<A>().HasMany(x => x.BCollection).WithMany(y => y.ACollection).Map(m => m.ToTable("My_Custom_Name")); } 
+4
source share
1 answer

Try also to determine the column names.

  modelBuilder.Entity<A>() .HasMany(x => x.BCollection).WithMany(y => y.ACollection) .Map(m => { m.ToTable("My_Custom_Name"); m.MapLeftKey("AId"); m.MapRightKey("BId"); }); 
+12
source

Source: https://habr.com/ru/post/1390678/


All Articles