Is it possible to specify the table name 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 that this could maybe work but apparently it doesn't.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<A>().HasMany(x => x.BCollection).WithMany(y => y.ACollection).Map(m => m.ToTable("My_Custom_Name"));
}
Try maapping the column names also.
modelBuilder.Entity<A>()
.HasMany(x => x.BCollection).WithMany(y => y.ACollection)
.Map(m =>
{
m.ToTable("My_Custom_Name");
m.MapLeftKey("AId");
m.MapRightKey("BId");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With