Linq 当我们连接到MySql时,Any()方法不起作用
当我们连接到MySql时,调用表上的Any()方法检查记录是否存在会引发异常,但当我们连接到SqlServer时,它工作正常 我正在使用以下代码:Linq 当我们连接到MySql时,Any()方法不起作用,linq,entity-framework-core,Linq,Entity Framework Core,当我们连接到MySql时,调用表上的Any()方法检查记录是否存在会引发异常,但当我们连接到SqlServer时,它工作正常 我正在使用以下代码: public IActionResult GetCustomers() { DbContext db = new DbContext(); if(db.Customers.Any())//here throws an exception when connect to mysql db { //my code }
public IActionResult GetCustomers()
{
DbContext db = new DbContext();
if(db.Customers.Any())//here throws an exception when connect to mysql db
{
//my code
}
return view();
}
我的例外是:
Exception has occurred: CLR/System.InvalidOperationException
An exception of type 'System.InvalidOperationException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code: 'No coercion operator is defined between types 'System.Int16' and 'System.Boolean'.'
at System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType)
at System.Linq.Expressions.Expression.Convert(Expression expression, Type type, MethodInfo method)
at System.Linq.Expressions.Expression.Convert(Expression expression, Type type)
at Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.CreateGetValueExpression(Expression dataReaderExpression, Int32 index, TypeMaterializationInfo materializationInfo, Boolean detailedErrorsEnabled, Boolean box)
at Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.<>c__DisplayClass13_0.<CreateArrayInitializer>b__0(TypeMaterializationInfo mi, Int32 i)
at System.Linq.Enumerable.<SelectIterator>d__154`2.MoveNext()
at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at System.Dynamic.Utils.CollectionExtensions.ToReadOnly[T](IEnumerable`1 enumerable)
at System.Linq.Expressions.Expression.NewArrayInit(Type type, IEnumerable`1 initializers)
at Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.CreateArrayInitializer(CacheKey cacheKey, Boolean detailedErrorsEnabled)
at Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.<Create>b__11_0(CacheKey k)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.Create(IReadOnlyList`1 types)
at Microsoft.EntityFrameworkCore.Query.Sql.DefaultQuerySqlGenerator.CreateValueBufferFactory(IRelationalValueBufferFactoryFactory relationalValueBufferFactoryFactory, DbDataReader dataReader)
at Microsoft.EntityFrameworkCore.Query.Internal.ShaperCommandContext.<NotifyReaderCreated>b__14_0(FactoryAndReader s)
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.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.GetResult[TResult](IEnumerable`1 valueBuffers, Boolean throwOnNullResult)
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ResultEnumerable`1.GetEnumerator()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass15_1`1.<CompileQueryCore>b__0(QueryContext qc)
at System.Linq.Queryable.Any[TSource](IQueryable`1 source, Expression`1 predicate)
at SampleEfCoreLatest.Controllers.HomeController.GetCustomers() in D:\SampleEfCoreLatest\Controllers\HomeController.cs:line 49
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()
发生异常:CLR/System.InvalidOperationException
Microsoft.EntityFrameworkCore.dll中发生类型为“System.InvalidOperationException”的异常,但未在用户代码中处理:“未在类型“System.Int16”和“System.Boolean”之间定义强制运算符。”
位于System.Linq.Expressions.Expression.GetUserDefinedImpressionOrthRow(ExpressionType强制类型,表达式表达式,类型convertToType)
at System.Linq.Expressions.Expression.Convert(表达式表达式、类型、方法信息方法)
at System.Linq.Expressions.Expression.Convert(表达式表达式,类型)
位于Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.CreateGetValueExpression(表达式dataReaderExpression,Int32索引,TypeMaterializationInfo materializationInfo,布尔详细信息ErrorEnabled,布尔框)
在Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.c__显示类别13_0.b__0(TypeMaterializationInfo mi,Int32 i)
在System.Linq.Enumerable.d_u154`2.MoveNext()中
位于System.Collections.Generic.LargearyBuilder`1.AddRange(IEnumerable`1项)
at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1源)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1源)
在System.Dynamic.Utils.CollectionExtensions.ToReadOnly[T](IEnumerable`1可枚举)
位于System.Linq.Expressions.Expression.NewArrayInit(类型类型,IEnumerable`1初始值设定项)
位于Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.CreateArrayInitializer(CacheKey CacheKey,Boolean DetailedErrorEnabled)
位于Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.b__11_0(缓存键k)
位于System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey,Func`2 valueFactory)
在Microsoft.EntityFrameworkCore.Storage.TypedRelationalValueBufferFactoryFactory.Create(IReadOnlyList`1类型)中
在Microsoft.EntityFrameworkCore.Query.Sql.DefaultQuerySqlGenerator.CreateValueBufferFactory(IRelationalValueBufferFactoryFactory RelationalValueBufferFactory,DbDataReader dataReader)中
位于Microsoft.EntityFrameworkCore.Query.Internal.ShaperCommandContext.b_u14_0(FactoryAndReader)
在Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue和target,TParam参数,Func`2 valueFactory)
位于Microsoft.EntityFrameworkCore.Query.Internal.ShaperCommandContext.NotifyReaderCreated(DbDataReader dataReader)
位于Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMovenText(DbContext),布尔缓冲区)
在Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()中
在Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.GetResult[TResult](IEnumerable`1 valueBuffers,Boolean throwOnNullResult)
位于Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ResultEnumerable`1.GetEnumerator()
位于Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
在System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source,Boolean&found)
在System.Linq.Enumerable.First[TSource](IEnumerable`1 source)处
在Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.c__显示Class15_1`1.b__0(QueryContext qc)
at System.Linq.Queryable.Any[TSource](IQueryable`1 source,表达式`1谓词)
在D:\SampleEfCoreLatest\Controllers\HomeController.cs中的SampleEfCoreLatest.Controllers.HomeController.GetCustomers()处:第49行
位于Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(对象目标,对象[]参数)
位于Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper映射器、ObjectMethodExecutor执行器、对象控制器、对象[]参数)
在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d_u12.MoveNext()中
您是否也会得到以下异常:
bool b=dbContext.Customers.Any()代码>如果您尝试:bool b=dbContext.Customers.Take(1.Count()!=0
,这实际上是Any()
所做的,感谢您的回答!dbContext.Customers.Any(i=>i.CustomerId>0);也会引发相同的异常。为此,我们可以使用bool b=dbContext.Customers.FirstOrDefault(i=>i.CustomerId>0)!=无效的但是我的问题是为什么Any()不能与mysql一起工作。得到了解决方案我们必须使用Pomelo.EntityFrameworkCore.mysql包而不是mysql.Data.EntityFrameworkCore包您是否也得到了以下例外:bool b=dbContext.Customers.Any()代码>如果您尝试:bool b=dbContext.Customers.Take(1.Count()!=0
,这实际上是Any()
所做的,感谢您的回答!dbContext.Customers.Any(i=>i.CustomerId>0);也会引发相同的异常。为此,我们可以使用bool b=dbContext.Customers.FirstOrDefault(i=>i.CustomerId>0)!=无效的但是我的问题是为什么Any()不能与mysql一起工作。找到了解决方案我们必须使用Pomelo.EntityFrameworkCore.mysql包而不是mysql.Data.EntityFrameworkCore包