I'm having an issue with EF6 and many-to-many relations. I have a following setup:
public class Foo
{
public int Id { get; set; }
public virtual ICollection<Bar> Bars { get; set; }
public virtual ICollection<SubBar> SubBars { get; set; }
public Foo()
{
Bars = new HashSet<Bar>();
SubBars = new HashSet<SubBar>();
}
}
public class Bar
{
public int Id { get; set; }
public virtual ICollection<Foo> Foos { get; set; }
public Bar()
{
Foos = new HashSet<Foo>();
}
}
public class SubBar
{
public int Id { get; set; }
public virtual ICollection<Foo> Foos { get; set; }
public SubBar()
{
Foos = new HashSet<Foo>();
}
}
The relation between Foo
and Bar
works properly, and there is a table called BarFoos
in the DB. However the relation between Foo
and SubBar
is reversed. There is a table in the DB called FooSubBars
which basically holds the correct relations, but eager loading, like SubBar.AsQueryable().Include(sb => sb.Foos)
returns an EntityCommandExecutionException
with a message Invalid object name dbo.SubBarFoos
.
Question is: How do I reverse the relation table name to allow eager loading?
--Edit--
DB creation in the migration is as follows:
CreateTable(
"dbo.BarFoos",
c => new
{
Bar_Id = c.Int(nullable: false),
Foo_Id = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.Bar_Id, t.Foo_Id })
.ForeignKey("dbo.Bars", t => t.Bar_Id, cascadeDelete: true)
.ForeignKey("dbo.Foos", t => t.Foo_Id, cascadeDelete: true)
.Index(t => t.Bar_Id)
.Index(t => t.Foo_Id);
CreateTable(
"dbo.FooSubBars",
c => new
{
Foo_Id = c.Int(nullable: false),
SubBar_Id = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.Foo_Id, t.SubBar_Id })
.ForeignKey("dbo.Foos", t => t.Foo_Id, cascadeDelete: true)
.ForeignKey("dbo.SubBars", t => t.SubBar_Id, cascadeDelete: true)
.Index(t => t.Foo_Id)
.Index(t => t.SubBar_Id);
Would replacing the table name in the migration be sufficient?
I was able to reverse the naming by checking here
https://msdn.microsoft.com/en-us/library/jj591620(v=vs.113).aspx
So long as both Entity1 and Entity2 have navagational properties to eachother, then the following works.
which produces
You can ensure the table name is generated as "FooBars" if you override the OnModelCreating method on your DbContext as follows:
Which will produce this in the migration:
I also tried eager loading the Foos when querying SubBars on the DbContext with don't get any errors: