私はこれをやっています:
User user = _usersRepository.SingleOrDefault(u => u.Username == "gigi", new string[] { "Roles" });
UnitOfWork.Current.Dispose();
そして、そのユーザーに関連付けられたロールを取得しません。そのSystem.ObjectDisposedExceptionを取得します。
そのリポジトリ呼び出しを行う代わりに、私は単に次のことを行います。
User user;
using (MyEntities ctx = new MyEntities ())
{
user = ctx.Users.Include("Roles").SingleOrDefault(u => u.Username == "gigi");
}
私は役割を取得します。私は何が欠けていますか?
LE:これは私のベースリポジトリがどのように見えるかです:
public class BaseRepository<T> : IBaseRepository<T> where T : class
{
private DbContext _context;
private IDbSet<T> _dbSet;
protected DbContext Context
{
get
{
if (_context == null)
{
EFUnitOfWork currentUnitOfWork = (EFUnitOfWork)UnitOfWork.Current;
_context = currentUnitOfWork.Context;
}
return _context;
}
}
protected IDbSet<T> DbSet
{
get
{
if (_dbSet == null)
{
_dbSet = Context.Set<T>();
}
return _dbSet;
}
}
public void Add(T entity)
{
DbSet.Add(entity);
}
public void Attach(T entity)
{
DbSet.Attach(entity);
}
public void Delete(T entity)
{
DbSet.Remove(entity);
}
public void Update(T entity)
{
Context.Entry(entity).State = System.Data.EntityState.Modified;
}
public T SingleOrDefault(Expression<Func<T, bool>> where, string[] includes=null)
{
if (includes != null)
{
foreach (string property in includes)
{
DbSet.Include(property);
}
}
return DbSet.SingleOrDefault(where);
}
public IQueryable<T> Get(Expression<Func<T, bool>> where, string[] includes=null)
{
if (includes != null)
{
foreach (string property in includes)
{
DbSet.Include(property);
}
}
return DbSet.Where(where);
}
}