簡體   English   中英

包含使用 SingleOrDefault 時的 EF Core 3.0 MoveNext 錯誤

[英]EF Core 3.0 MoveNext Error when Include using SingleOrDefault

我閱讀了不同的解決方案並嘗試了不同的實現,但沒有任何結果。 使用不同的實現,錯誤總是相同的:“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 指南會產生相同的錯誤。 謝謝你的時間。

  1. 我認為擁有一個可用於導航和定義Portfolio class 和Trade class 之間的關系的公共Portfolio.Trades屬性真的會幫助您。
    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對象作為查詢的一部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM