如何使用存储库模式在实体框架中执行复杂的查询(例如,通过多对多关系)?
我正在使用实体框架 v4 和在这个问题的答案中找到的存储库模式:
也就是说,每个存储库都继承自一个抽象基类,该基类包含添加、删除等通用方法。并且还为该存储库/实体特有的任何方法实现特定的存储库接口。
IE。
public class CompanyRepository : Repository<Company>, ICompanyRepository {
public Company Get(int id)
{
return base.ObjectSet.SingleOrDefault(x => x.Id == id);
}
}
如何执行更复杂的查询来引用不属于此存储库的实体...(例如与另一个表的多对多关系)?
public List<Company> GetAllCompaniesForUser(int userId)
{
return base.ObjectSet.Where(x => x.Users ?????
}
I am using Entity Framework v4 and the repository pattern found in the answer to this SO question:
Advantage of creating a generic repository vs. specific repository for each object?
Namely, each repository inherits from an abstract base class that contains generic methods like add, delete, etc. and also implements a specific repository interface for any methods that are unique to that repository/entity.
ie.
public class CompanyRepository : Repository<Company>, ICompanyRepository {
public Company Get(int id)
{
return base.ObjectSet.SingleOrDefault(x => x.Id == id);
}
}
How can I perform more complex queries that reference entities that are not part of this repository... (say across a many-to-many relationship to another table)?
public List<Company> GetAllCompaniesForUser(int userId)
{
return base.ObjectSet.Where(x => x.Users ?????
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这可能是这样做的方法:
或者
I think this may be the way to do it:
or