C# “使用多个DbContext获取错误”无法在单个查询执行中使用多个DbContext实例
我使用.NETCore3启动了一个新的MVC应用程序。我有三个DbContext文件,它们使用三个不同的数据库:主应用程序的ComplaintDbContext、身份用户的IdentityCoreDbContext和员工数据库的EmployeeDbContext 在我的应用程序中,我有一个名为ComplaintRepository的存储库类,构造函数如下所示:C# “使用多个DbContext获取错误”无法在单个查询执行中使用多个DbContext实例,c#,asp.net-core-mvc,entity-framework-core,C#,Asp.net Core Mvc,Entity Framework Core,我使用.NETCore3启动了一个新的MVC应用程序。我有三个DbContext文件,它们使用三个不同的数据库:主应用程序的ComplaintDbContext、身份用户的IdentityCoreDbContext和员工数据库的EmployeeDbContext 在我的应用程序中,我有一个名为ComplaintRepository的存储库类,构造函数如下所示: public ComplaintRepository(ComplaintDbContext context, Empl
public ComplaintRepository(ComplaintDbContext context,
EmployeeDbContext employeeContext)
{
_context = context;
_employeeContext = employeeContext;
}
public class FrameworkContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
}
public class ExtendedContext : FrameworkContext
{
public DbSet<Order> Orders { get; set; }
}
在ComplaintController中,我需要从两个数据库获取数据。我可以从投诉数据库中获取数据,但一旦我调用从员工数据库中获取数据的操作,我就会得到错误:
无法在单个查询执行中使用多个DbContext实例。确保查询使用单个上下文实例
我试过这样的方法:
public ComplaintRepository(ComplaintDbContext context,
EmployeeDbContext employeeContext)
{
_context = context;
_employeeContext = employeeContext;
}
public class FrameworkContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
}
public class ExtendedContext : FrameworkContext
{
public DbSet<Order> Orders { get; set; }
}
public类FrameworkContext:DbContext
{
公共数据库集客户{get;set;}
}
公共类ExtendedContext:FrameworkContext
{
公共数据库集命令{get;set;}
}
我不能让它工作。任何帮助都将不胜感激。谢谢
编辑:
我创建了一个名为EmployeeRepository的新存储库来分离关注点。以下是给我带来问题的行动:
public IEnumerable<ApplicationUser> GetWorkerList()
{
var employees = _employeeRepository.GetEmployeesByUnit(22);
//Get ApplicationUsers where user exists in Employees list
IEnumerable<ApplicationUser> userList = _userManager.Users
.Where(emp => employees.Any(e => emp.EmployeeID == e.EmployeeId)).OrderBy(e => e.LastName);
return userList;
}
public IEnumerable GetWorkerList()
{
var employeers=_employeeRepository.GetEmployeesByUnit(22);
//获取用户存在于员工列表中的ApplicationUsers
IEnumerable userList=\u userManager.Users
.Where(emp=>employees.Any(e=>emp.EmployeeID==e.EmployeeID)).OrderBy(e=>e.LastName);
返回用户列表;
}
我的员工数据库和我的身份数据库都共享一个名为EmployeeId的列
当我尝试将其更改为使用ToList()时,我开始出现另一个错误:
InvalidOperationException:无法翻译LINQ表达式“Where(源:DbSet,谓词:(a)=>Any(源:(未处理的参数:\uu employees\u 0),谓词:(e)=>a.EmployeeID==e.EmployeeID))”。以可以翻译的形式重写查询,或者通过插入对AsEnumerable()、AsAsAsAsyncEnumerable()、ToList()或ToListSync()的调用显式切换到客户端计算
编辑:
我使用了陶舟推荐的使用ToList()的方法,并且我能够让它正常工作。我替换了存储库中的IEnumerable以使用以下内容:
public List<TblEmployee> GetEmployeesByUnit(int unitId)
{
var emp = _context.TblEmployee.Where(e => e.UnitId == unitId &&
e.TermDate == null)
.OrderBy(e => e.LastName).ToList();
return emp;
}
公共列表GetEmployeesByUnit(int-unitId)
{
var emp=_context.TblEmployee.Where(e=>e.UnitId==UnitId&&
e、 TermDate==null)
.OrderBy(e=>e.LastName).ToList();
返回emp;
}
在我的控制器中,我基本上也这样做了,现在我有了:
public List<ApplicationUser> GetWorkerList()
{
var employees = _employeeRepository.GetEmployeesByUnit(22);
List<ApplicationUser> userList = new List<ApplicationUser>();
//Get ApplicationUsers where user exists in Employees list
foreach (TblEmployee emp in employees)
{
ApplicationUser user = _userManager.Users
.Where(e => e.EmployeeID == emp.EmployeeId).FirstOrDefault();
userList.Add(user);
}
return userList;
}
public List GetWorkerList()
{
var employeers=_employeeRepository.GetEmployeesByUnit(22);
List userList=新列表();
//获取用户存在于员工列表中的ApplicationUsers
foreach(员工中的员工emp)
{
ApplicationUser user=\u userManager.Users
其中(e=>e.EmployeeID==emp.EmployeeID).FirstOrDefault();
添加(用户);
}
返回用户列表;
}
我希望使用LINQ而不是foreach循环。我遇到了同样的问题,我使用异步等待结构解决了这个问题。 我有2个查询,它给出了错误
var result = await _context.YourTable.ToListAsync();
也许这会对您有所帮助。您说您正在使用三个不同的数据库,但随后引用了三个不同的数据库上下文。这些DbContexts是否映射到同一个SQL数据库?请与我们共享您的查询代码。您无法在同一查询中组合
ComplaintDbContext
和EmployeeDbContext
,请尝试使用ToList()
从它们中进行查询,然后从列表结果中进行查询。CPerson,这三个DBContext不映射到同一个数据库。我在使用的同一台SQL server上有三个不同的数据库。谢谢。陶舟,我试过使用ToList(),现在我发现了一个不同的错误。请看我的编辑。谢谢。关于\u userManager.Users.Where(emp=>employees.Any(e=>emp.EmployeeID==e.EmployeeID)).OrderBy(e=>e.LastName.ToList()
?