序列包含多个与FromSql匹配的元素

时间:2018-11-23 20:32:43

标签: sql-server entity-framework ef-core-2.0

我得到了

  

System.InvalidOperationException:'序列包含多个匹配元素'

System.InvalidOperationException
  HResult=0x80131509
  Message=Sequence contains more than one matching element
  Source=System.Linq
  StackTrace:
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at Microsoft.EntityFrameworkCore.Query.Sql.Internal.FromSqlNonComposedQuerySqlGenerator.CreateValueBufferFactory(IRelationalValueBufferFactoryFactory relationalValueBufferFactoryFactory, DbDataReader dataReader)
   at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue& target, TParam param, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Query.Internal.ShaperCommandContext.NotifyReaderCreated(DbDataReader dataReader)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at SourceLine.Api.Services.InventoryService.GetInventoryItems(Int32 categoryId, String searchCriteria) in InventoryService.cs:line 216
   at SourceLine.Api.Controllers.InventoriesController.GetInventoryItemsBySearch(Int32 categoryId, String search) in InventoriesController.cs:line 73
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()

这是我的代码:

var items = dbContext.Items
                     .AsNoTracking()
                     .FromSql(@"SELECT *
                                FROM Branches 
                                INNER JOIN Brands ON Branches.Id = Brands.Id 
                                INNER JOIN Companies ON Branches.CompanyId = Companies.Id  
                                INNER JOIN Countries ON Branches.CountryId = Countries.Id 
                                INNER JOIN InventoryItems ON Branches.Id = InventoryItems.BranchId AND Brands.Id = InventoryItems.BrandId 
                                INNER JOIN States ON Branches.StateId = States.Id AND Countries.Id = States.CountryId
                                WHERE JSON_VALUE(InventoryItems.Attributes, '$.Column') LIKE @search", searchCriteriaParam).ToList();

我不确定为什么。

编辑

我将其从“选择*”更改为

SELECT InventoryItems.Id, Branches.Id

0 个答案:

没有答案
相关问题