c# - 包含使用 SingleOrDefault 时的 EF Core 3.0 MoveNext 错误
问题描述
我阅读了不同的解决方案并尝试了不同的实现,但没有任何结果。使用不同的实现,错误总是相同的:“System.InvalidOperationException:Enumerator failed to MoveNextAsync。”
这是产生异常的地方。
var portfolioTrades = await _context
.Portfolios
.Include(PortfolioEntityTypeConfiguration.TradesList)
.SingleOrDefaultAsync(x => x.id == id);
包含以方式处理
builder.OwnsMany<Trade>(TradesList, x =>
{
x.WithOwner().HasForeignKey("portfolio_id");
x.ToTable("product_trade", SchemaNames.Public);
x.Property<TradeID>("id");
x.Property<DateTimeOffset>("_date").HasColumnName("date");
x.Property("_details").HasColumnName("details");
x.Property<Guid>("_schemaId").HasColumnName("schema_id");
x.HasKey(x => x.id);
});
EF 执行此查询并返回 1 条记录
SELECT t.id, t.description, t.end_client_name, t.name, t0.id, t0.details, t0.portfolio_id
FROM (
SELECT p.id, p.description, p.end_client_name, p.name
FROM account.portfolio AS p
WHERE p.id = '3adcaff1-de64-4ae3-b8b7-c390d76aa0bd'
LIMIT 2
) AS t
LEFT JOIN product_trade AS t0 ON t.id = t0.portfolio_id
ORDER BY t.id, t0.id
这里下面的实体
public class Trade : Entity
{
public TradeID id { get; private set; }
private DateTimeOffset _date { get; set; }
public JObject _details { get; set; }
private Guid _schemaId { get; set; }
private Trade()
{
id = new TradeID(Guid.NewGuid());
}
private Trade( DateTimeOffset date, string details, Guid schema_id)
{
id = new TradeID(Guid.NewGuid());
_date = date;
_schemaId = schema_id;
_details = JsonConvert.DeserializeObject<JObject>(details);
}
internal static Trade Create(DateTimeOffset date, string details, Guid schema_id)
{
return new Trade(date, details, schema_id);
}
}
}
public class Portfolio : Entity, IAggregateRoot
{
public PortfolioID id { get; private set; }
private string _name { get; set; }
private string _end_client_name { get; set; }
private string _description { get; set; }
private readonly List<Trade> _trades;
private Portfolio()
{
_trades = new List<Trade>();
}
}
// 错误
System.InvalidOperationException: Enumerator failed to MoveNextAsync.
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at Rx.Products.Infrastructure.Domain.Portfolios.PortfolioRepository.GetByPortfolioIdAsync(PortfolioID id) in ....\PortfolioRepository.cs:line 37
at Rx.Products.Application.Portfolios.CreateTrade.CreateTradeCommandHandler.Handle(CreateTradeCommand request, CancellationToken cancellationToken) in ...\CreateTradeCommandHandler.cs:line 19
at MediatR.Pipeline.RequestPreProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
at MediatR.Pipeline.RequestPostProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
at Rx.Products.API.TradesController.RegisterCustomer(CreateTradeRequest new_trade) in ....\TradesController.cs:line 65
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
HEADERS
=======
Accept: */*
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 726
Content-Type: application/json
Host: localhost:54315
User-Agent: PostmanRuntime/7.24.1
Postman-Token: 2aabfa69-9d5c-4637-9d48-02a713077235
有关关系的ms 指南会产生相同的错误。谢谢你的时间。
解决方案
Portfolio.Trades
我认为拥有一个可用于导航并定义Portfolio
类与类之间关系的公共属性确实会帮助您Trade
。
public class Portfolio : Entity, IAggregateRoot
{
// Other class members defined above...
// New public navigation property for the relationship.
public IReadOnlyList<Trade> Trades => _trades.AsReadOnly();
}
- 如果使用
OwnsMany
关系,则不需要.Include
在查询中使用语句。如果您使用HasMany
关系,那么您将需要使用该.Include
语句。
使用HasMany
和Include
:
builder.HasMany(port.Trades, x =>
{
// Other configuration code defined above...
});
var portfolioTrades = await _context
.Portfolios
.Include(port => port.Trades)
.SingleOrDefaultAsync(port => port.id == id);
使用OwnsMany
和不使用Include
:
builder.OwnsMany(port.Trades, x =>
{
// Other configuration code defined above...
});
var portfolioTrades = await _context
.Portfolios
.SingleOrDefaultAsync(port => port.id == id);
使用OwnsMany
关系意味着Trade
将自动为您查询所有子对象。使用HasMany
关系允许您指定包含子Trade
对象作为查询的一部分。
推荐阅读
- ruby-on-rails - Uglifier:在 Heroku 上推送失败 - Rails 5.2.3
- angular - 使用自定义类中的 Angular MatDialog(不包含在 app.module.ts 中)
- react-native-android - REACT-NATİVE - jarsigner 错误:java.lang.RuntimeException:密钥库加载:密钥库被篡改,或密码不正确
- php - 如何在 STRIPE API 中获取客户的失败发票列表?
- java - 在这种情况下使用泛型时如何让spring注入正确的bean?
- git - 如果源(功能)分支在不久之后被删除,那么 --squash 合并和 --no-ff 合并之间有区别吗?
- node.js - 在 Node.js 项目中解析 OpenXML Office 文档
- c# - SSIS 错误 - 找不到脚本的二进制代码
- xamarin - 如何解决“无法解析主机”错误?
- swift - 带有 SF 符号的 HStack 图像未居中对齐