LINQ+EF:Cannot use multiple context instances within a single query execution. Ensure the query uses a single context instance.
System.InvalidOperationException:“Cannot use multiple context instances within a single query execution. Ensure the query uses a single context instance.”
一个LINQ查询不允许有多个不同数据库上下文实例。
解决方案
将两个数据库的LINQ查询结果ToList(),然后关联List处理数据。
C# 全选
//1.查询账套数据库,获取销售单数据
var q = from a in _Database.GetQueryable<tb_SO>()
where a.FlagApp == "Y" && a.DocDate >= DateTime.Today.AddMonths(-1)//统计已审核,1个月订单
group a by a.CreatedBy into g
select new HomeDataSalesAmount
{
UserId = g.Key,
Rows = g.Count(),
Amount = g.Sum(s => s.TotalAmount ?? 0),
};
var listSalesRank = q.Where(w => w.Amount > 0).OrderByDescending(o => o.Amount).ToList();
listSalesRank.ForEach(item => item.Rank = listSalesRank.IndexOf(item) + 1);//排名
//2.查询系统数据库,获取用户表
var listUsers = DatabaseProvider.SystemDatabase.GetQueryable<tb_MyUser>()
.Select(s => new { s.Account, s.UserName }).ToList();
//3.关联起来,更新姓名
(from a in listSalesRank
join b in listUsers on a.UserId equals b.Account
select new { a, b }).ForEach(item =>
{
item.a.UserName = item.b.UserName;
});
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网