C# LINQ抱怨在实现多个存储库的服务中使用了不同的EF上下文
我有一个我的存储库继承的BaseRepository。代码声明如下:C# LINQ抱怨在实现多个存储库的服务中使用了不同的EF上下文,c#,entity-framework,linq,C#,Entity Framework,Linq,我有一个我的存储库继承的BaseRepository。代码声明如下: public interface IBaseRepository<T> : where T : class { IQueryable<T> GetAll(); } public abstract class BaseRepository<C, T> : IBaseRepository<T> where T : class where C : DbCon
public interface IBaseRepository<T> : where T : class
{
IQueryable<T> GetAll();
}
public abstract class BaseRepository<C, T> : IBaseRepository<T>
where T : class
where C : DbContext, new()
{
protected BaseRepository()
{
_context = new C();
_context.Database.Log = message => Trace.WriteLine(message);
}
private readonly C _context;
protected C Context
{
get { return _context; }
}
public virtual IQueryable<T> GetAll()
{
return _context.Set<T>();
}
}
public interface IARepository : IBaseRepository<A>
{
}
public ARepository : BaseRepository<Entities, A>, IARepository
{
}
public interface IBRepository : IBaseRepository<B>
{
}
public ARepository : BaseRepository<Entities, B>, IBRepository
{
}
公共接口IBaseRepository:其中T:class
{
IQueryable GetAll();
}
公共抽象类BaseRepository:IBaseRepository
T:在哪里上课
其中C:DbContext,new()
{
受保护的BaseRepository()
{
_context=newc();
_context.Database.Log=message=>Trace.WriteLine(消息);
}
私有只读C_上下文;
受保护的C上下文
{
获取{return\u context;}
}
公共虚拟IQueryable GetAll()
{
返回_context.Set();
}
}
公共接口IARepository:IBaseRepository
{
}
公共存储库:BaseRepository,iRepository
{
}
公共接口IBRepository:IBaseRepository
{
}
公共存储库:BaseRepository、iRepository
{
}
然后我有一个serivce层,它将使用多个存储库为我的控制器获取数据
public class SomeService
{
private readonly IARepository _aRepository;
private readonly IBRepository _bRepository;
public EventService(IARepository aRepository, IBRepository bRepository)
{
_aRepository = aRepository;
_bRepository = bRepository;
}
public EventService() : this(new ARepository(), new BRepository())
{
}
public IEnumerable<SomeDTO> GetSomeDTOs()
{
return _aRepository.GetAll()
.Join(_bRepository.GetAll(), a => a.SomeId, b => b.SomeId, (c, d) => new SomeDTO
{
...
...
...
}).ToList();
}
}
公共类服务
{
私有只读存储库(存储库);;
私人只读易趣存储库;
公共事件服务(Irepository aRepository、IBRepository bRepository)
{
_aRepository=aRepository;
_bRepository=bRepository;
}
public EventService():此(新的ARepository(),新的BRepository())
{
}
公共IEnumerable GetSomeDTOs()
{
return _aRepository.GetAll()
.Join(_bRepository.GetAll(),a=>a.SomeId,b=>b.SomeId,(c,d)=>newsomedto
{
...
...
...
}).ToList();
}
}
但问题是。我得到以下错误:
“System.NotSupportedException”类型的首次意外异常
在EntityFramework.SqlServer.dll中发生
其他信息:指定的LINQ表达式包含
对与不同上下文关联的查询的引用
当我调用GetSomeDTOs函数时。据我所见,它应该使用与baserepository中声明的相同的上下文。这里似乎有什么问题?问题是每个存储库都有自己的上下文,因此不能将它们结合在一起。一个简单的修复方法是创建共享上下文并将其传递到存储库:
public abstract class BaseRepository<C, T> : IBaseRepository<T>
where T : class
where C : DbContext
{
protected BaseRepository(C context)
{
_context = context;
_context.Database.Log = message => Trace.WriteLine(message);
}
//snip
}
啊,
\u是position
和\u breposition
有各自的上下文对象。如果你想把它们连接在一起,它们需要引用同一个。我该怎么做?存储库和服务位于没有应用程序的类库中;哦,所以你有一个IoC容器。所以,你需要告诉它,他们之间的上下文是共享的。啊,太简单了。非常感谢!
var context = new MyDbContext();
IARepository aRepository = new ARepository(context);
IBRepository bRepository = new BRepository(context);