LINQ to Entities - 使用代码优先的多对多查询
假设我有这个:
public partial class AccessMask
{
public int AccessMaskID { get; set; }
=== bla bla bla ===
public virtual ICollection<Role> Role { get; set; }
}
public partial class Role
{
public int RoleID { get; set; }
=== bla bla bla==
public virtual ICollection<Forum> Forum { get; set; }
public virtual ICollection<Rank> Rank { get; set; }
public virtual ICollection<User> User { get; set; }
public virtual ICollection<AccessMask> AccessMask { get; set; }
}
现在有一个大问题。如何在这两者之间进行查询?正如您所看到的,这是多对多的关系。我想做的是加入这个并获得与角色连接的访问掩码。 bla bla bla-一些完全不相关的随机属性。
更新!
我知道我可以使用: 包括() 但是将它用于大量查询......将是一个坏主意。因为我有很多看起来相似的多对多关系。 或者我是否必须简单地重新设计我的模型,使其成为更传统的连接实体?
Let's say i have this:
public partial class AccessMask
{
public int AccessMaskID { get; set; }
=== bla bla bla ===
public virtual ICollection<Role> Role { get; set; }
}
public partial class Role
{
public int RoleID { get; set; }
=== bla bla bla==
public virtual ICollection<Forum> Forum { get; set; }
public virtual ICollection<Rank> Rank { get; set; }
public virtual ICollection<User> User { get; set; }
public virtual ICollection<AccessMask> AccessMask { get; set; }
}
And now a big question. How to make query between those two? As you may see it's many-to-many relation. And What I want to do is to join this and get access mask connected with role.
bla bla bla- some random properties that are completly irrevelant.
UPDATE!
I know I can use:
Include()
But using it for lot's of queries.. will be a bad idea. Because I have quite a few many-to-many relationships that look similiar.
Or Do I have to simply redeisgn my model, to something more traditional with joining entity ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于检索数据,它的工作方式与任何其他导航属性相同:
但是,我在保存多对多数据方面经历了一段非常艰难的时间。
For retrieving data, it will work the same way as any other navigation property:
However, I have had a very rough time of getting many-to-many data to save.