【发布时间】:2010-06-16 17:16:28
【问题描述】:
我的场景:
这是一个通过 C# 编程的 ASP.NET 4.0 Web 应用程序
我实现了一个存储库模式。我的存储库都共享相同的ObjectContext,它存储在 httpContext.Items 中。每个存储库都会创建一个 E 类型的新 ObjectSet。以下是我的存储库中的一些代码:
public class Repository<E> : IRepository<E>, IDisposable
where E : class
{
private DataModelContainer _context = ContextHelper<DataModelContainer>.GetCurrentContext();
private IObjectSet<E> _objectSet;
private IObjectSet<E> objectSet
{
get
{
if (_objectSet == null)
{
_objectSet = this._context.CreateObjectSet<E>();
}
return _objectSet;
}
}
public IQueryable<E> GetQuery()
{
return objectSet;
}
假设我有 2 个存储库,1 个用于州,1 个用于国家,并且想要针对两者创建一个 linq 查询。请注意,我将 POCO 类与实体框架一起使用。 State 和 Country 是这些 POCO 类中的 2 个。
Repository stateRepo = new Repository<State>();
Repository countryRepo = new Repository<Country>();
IEnumerable<State> states = (from s in _stateRepo.GetQuery()
join c in _countryRepo.GetQuery() on s.countryID equals c.countryID
select s).ToList();
Debug.WriteLine(states.First().Country.country)
基本上,我想检索州和相关国家实体。查询只返回状态数据...我在Debug.WriteLine 上得到一个空参数异常
LazyLoading 在我的 .edmx 中被禁用...这就是我想要的方式。
【问题讨论】:
标签: asp.net linq entity-framework linq-to-entities repository