c# - “处理 LINQ 表达式 'GroupByShaperExpression” 升级到 ef core 后出错。5
问题描述
我的项目是 .net core 2.2 并升级到 .net 5 和 EF 5.0 我的最后一个查询是
public IList<Group<String, UsefullLink>> GetAllGroupActive()
{
return (from b in _db.UsefullLinks
group b by b.UseFullLinkType.Name into g
select new Univer.Business.Model.Group<string, UsefullLink>
{
Key = g.Key,
Values = g
}).ToList();
}
我的模特
public class UsefullLink
{
public int Id { get; set; }
public string Name { get; set; }
public bool Active { get; set; }
public DateTime CreateDate { get; set; }
public string Image { get; set; }
public int LinkTypeId { get; set; }
public LinkType LinkType { get; set; }
public int Priority { get; set; }
...
public string Description { get; set; }
public int UseFullLinkTypeId { get; set; }
public UseFullLinkType UseFullLinkType { get; set; }
}
public class UseFullLinkType
{
public int Id { get; set; }
[Required, DataType(DataType.Text)]
public string Name { get; set; }
[DataType(DataType.MultilineText)]
public string Description { get; set; }
[Required, DataType(DataType.Date)]
public DateTime InputDate { get; set; }
public int Priority { get; set; }
public ICollection<UsefullLink> UsefullLinks { get; set; }
}
我的观点
@using Univer.Business.Model
@model List<Group<string, Univer.Model.UsefullLink>>
@{
ViewData["Title"] = "ServiceTebbedTable";
}
<div class="container">
<div class="row UsefullLink ">
<div class="card ">
<div class="card-header"> <a asp-action="ServiseTable" asp-controller="Home" asp-area="">service table</a></div>
<ul class="row" style="margin-left:25px ;margin-right:25px;">
@foreach (var group in Model)
{
<li class="row col-md-12" id="cut-diamond"> @group.Key</li>
<li class="m-1"></li>
foreach (var link in group.Values)
{
<li class="col-md-4 mt-2 justify-content-center">
@if (link.Image != "1")
{
<img src="@Url.Content(link.Image)" class="UsefullLink-icon" />
}
else
{
<img src="~/images/Logo200blue.png" class="UsefullLink-icon" />
}
<a href="@link.LinkUrl" class="justify-content-center" target="_blank"> @link.Name</a>
</li>
}
}
</ul>
</div>
</div>
</div>
运行项目后我收到此错误
InvalidOperationException:处理 LINQ 表达式'GroupByShaperExpression:KeySelector:u.Name,ElementSelector:EntityShaperExpression:EntityType:UsefullLink ValueBufferExpression:ProjectionBindingExpression:EmptyProjectionMember IsNullable:False
UPDATE1: 对于组,我在 Univer.Business.Model 中有这个
public class Group<k, T>
{
public k Key;
public IEnumerable<T> Values;
}
Update2 这是我的存储库公共类 UsefullLinkRepository 的构造函数: IUsefullLinkRepository { private readonly UniverContext _db;
public UsefullLinkRepository(UniverContext db)
{
_db = db;
}
}
我将它的方法更改为
var data = from b in _db.UsefullLinks.AsEnumerable()
join c in _db.UseFullLinkTypes on b.UseFullLinkTypeId equals c.Id
group b by b.UseFullLinkType.Name into g
select g;
return data.Select(g => new Group<string, UsefullLink>
{
Key = g.Key,
Values = g
}).ToList();
运行Update2后 出现此错误
InvalidOperationException:已经有一个打开的 DataReader 与此 Connection 关联,必须先关闭它。
解决方案
这里的问题是 EF 2.x 在客户端静默评估不可翻译的查询。在这种情况下,EF Core 5 不会抛出正确的异常是一个错误。
考虑重写您的查询
var grouped = from b in _db.UsefullLinks.AsEnumerable()
group b by b.UseFullLinkType.Name into g
select g;
return grouped.Select(g => new Univer.Business.Model.Group<string, UsefullLink>
{
Key = g.Key,
Values = g // this assignment is not translatable
}).ToList();
推荐阅读
- angular - 将对象属性绑定到输入并将值传递给以角度单击按钮时的功能
- php - 为嵌套数组检查 in_array
- javascript - 如何在 Vue.js 中将特定颜色传递给父组件?
- php - DOCUMENT_ROOT 的使用
- javascript - 点击 JQuery
- 干涉
- thymeleaf - 如何将整个 html 文件包含到 thymeleaf 中的另一个文件中,而不仅仅是特定的 div?
- apache-spark - 如何在火花中创建特定的时间范围
- php - 我无法删除数据透视表中存在的记录
- java - 如何解决在 Spring Webflux 中将一个数据放入单声道到另一个单声道的问题?
- c# - UWP 平铺图像拉伸和填充