public class MyContext : DbContext
{
public DbSet<Foo> Foos {get;set;}
public DbSet<Bar> Bars {get;set;}
//some model creation about many to many (i ommitted it for simplicity)
}
public class Foo
{
public long Id {get;set;}
public virtual ICollection<Bar> Bars {get;set;}
}
public class Bar
{
public long Id {get;set;}
public virtual ICollection<Foo> Foos {get;set;}
}
这将创建3个表:Foos,Bars,FooBars(关系表)
那么,只有一个类型为Foo
的对象和Bar
id的列表,如何在不首先通过EF6中的选择查询请求整个对象的情况下将这些多对多id插入关系表中?
var barIds = new List<long>(){ /*...*/};
var foo = ctx.Foos.Single(x=> x.Id == 666);
foo.Bars = ctx.Bars.Select(x=> barIds.Contains(x.Id)).ToList();//this is hilarious query and very inefficient