首页 > 解决方案 > 包含使用 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 指南会产生相同的错误。谢谢你的时间。

标签: c#ef-core-3.0

解决方案


  1. 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();
    }
  1. 如果使用OwnsMany关系,则不需要.Include在查询中使用语句。如果您使用HasMany关系,那么您将需要使用该.Include语句。

使用HasManyInclude

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对象作为查询的一部分。


推荐阅读