سلام من وقتی متد Get به همراه ID فراخوانی میکنم خطای 500 زیر رخ میده:
System.InvalidOperationException: The type of navigation 'Customer.Orders' is 'IQueryable<Order>' for which it was not possible to create a concrete instance. Either initialize the property before use, add a public parameterless constructor to the type, or use a type which can be assigned a 'HashSet<>' or 'List<>'.
at Microsoft.EntityFrameworkCore.Metadata.Internal.ClrICollectionAccessor`3.GetOrCreateCollection(Object instance, Boolean forMaterialization)
at Microsoft.EntityFrameworkCore.Metadata.Internal.ClrICollectionAccessor`3.GetOrCreate(Object entity, Boolean forMaterialization)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.InitializeIncludeCollection[TParent,TNavigationEntity](Int32 collectionId, QueryContext queryContext, DbDataReader dbDataReader, SingleQueryResultCoordinator resultCoordinator, TParent entity, Func`3 parentIdentifier, Func`3 outerIdentifier, INavigationBase navigation, IClrCollectionAccessor clrCollectionAccessor, Boolean trackingQuery, Boolean setLoaded)
at lambda_method25(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.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 EshopApi.Servises.Implementation.CustomerRepository.Find(Int32 id) in C:\Users\Mohammad\source\repos\EshopApi\EshopApi\Servises\Implementation\CustomerRepository.cs:line 29
at EshopApi.Controllers.CustomersController.GetCustomer(Int32 id) in C:\Users\Mohammad\source\repos\EshopApi\EshopApi\Controllers\CustomersController.cs:line 34
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)
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__Logged|17_1(ResourceInvoker invoker)
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 Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
متد تعریف شده در کنترولر:
public async Task<IActionResult> GetCustomer([FromRoute] int id)
{
var customer = await _customer.Find(id);
return Ok(customer);
}
کد repository
public async Task<Customer> Find(int id)
{
return await _db.Customers.Include(x=>x.Orders).SingleOrDefaultAsync(i => i.Id == id);
}
سلام
متن خطا کاملا واضح هست
از مترجم گوگل استفاده کنید
مشکل حل کردم
کد قبلی من این بود :
public IQueryable<Order> Orders { get; set; }
که به کد زیر تغییرش دادم:
public List<Order> Orders { get; set; }
حالا میخوام بدونم چرا در حالت IQueryable خطا رخ میداد ؟