Sequence contains more than one matching element with FromSql

0

I am getting

System.InvalidOperationException: 'Sequence contains more than one matching element'

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()

This is my code:

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();

I am not sure why.

Edit

I changed it from Select * to

SELECT InventoryItems.Id, Branches.Id
sql-server
entity-framework
ef-core-2.0
asked on Stack Overflow Nov 23, 2018 by chobo2 • edited Nov 23, 2018 by chobo2

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0