INT []。含有不EF6工作 [英] Int[].Contains doesn't work in EF6

查看:153
本文介绍了INT []。含有不EF6工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,基本上我有这样的代码:

  VAR汉语语言=(从升的context.languages 
,其中Model.LanguageIDs.Contains(l.LanguageID)
选择L)
.ToList<语言>();



我不知道为什么,但我总是得到一个错误:




System.NullReferenceException:对象引用未设置为一个对象




<实例p >事情是在此之前的工作,但在那之后我已经升级了我所有的应用程序MVC5和EF6,它开始崩溃在此代码。



虽然如果我检查 Model.Langauges ,它有几个ID的所以这是确定的。



我也穿过高的背景下环。.Languages和所有的人都ID的



我也写过这样的:

  VAR汉语语言=(从升的context.languages 
,其中1 == l.LanguageID
选择L)
.ToList<语言>();

和这部作品也,所以我真的不明白,我究竟做错了什么?< ?/ p>

任何人都可以详细...



堆栈跟踪:

 在MySql.Data.Entity.SqlGenerator.Visit(DbPropertyExpression表达式)
在MySql.Data.Entity.SqlGenerator.Visit(DbInExpression表达式)
。在系统.Data.Entity.Core.Common.CommandTrees.DbInExpression.Accept [TResultType](DbExpressionVisitor`1游客)在MySql.Data.Entity.SqlGenerator.VisitBinaryExpression
(DbExpression左,右DbExpression,字符串操作)
。在MySql.Data.Entity.SqlGenerator.Visit(DbAndExpression表达式)
在MySQL的System.Data.Entity.Core.Common.CommandTrees.DbAndExpression.Accept [TResultType](DbExpressionVisitor`1游客)
.Data.Entity.SelectGenerator.Visit(DbFilterExpression表达式)
在在MySql.Data System.Data.Entity.Core.Common.CommandTrees.DbFilterExpression.Accept [TResultType](DbExpressionVisitor`1游客)
。 Entity.SqlGenerator.VisitInputExpression(DbExpression E,字符串名称,TypeUsage型)
在MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression E,字符串名称,TypeUsage型)
在MySql.Data.Entity。 SelectGenerator.Visit(DbProjectExpression表达式)
在在MySql.Data.Entity.SelectGenerator.GenerateSQL System.Data.Entity.Core.Common.CommandTrees.DbProjectExpression.Accept [TResultType](DbExpressionVisitor`1游客)
(DbCommandTree树)
在MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest,DbCommandTree commandTree)
在System.Data.Entity.Core.Common.DbProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest,DbCommandTree commandTree, DbInterceptionContext interceptionContext)
在System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree,DbInterceptionContext interceptionContext)
在System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition..ctor( DbProviderFactory storeProviderFactory,DbCommandTree commandTree,DbInterceptionContext interceptionContext,IDbDependencyResolver解析器,BridgeDataReaderFactory bridgeDataReaderFactory,columnMapFactory columnMapFactory)
在System.Data.Entity.Core.EntityClient.Internal.EntityProviderServices.CreateCommandDefinition(DbProviderFactory storeProviderFactory,DbCommandTree commandTree,DbInterceptionContext interceptionContext,IDbDependencyResolver解析)
在System.Data.Entity.Core.EntityClient.Internal.EntityProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest,DbCommandTree commandTree,DbInterceptionContext interceptionContext)
在System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition( DbCommandTree commandTree,DbInterceptionContext interceptionContext)在System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.CreateCommandDefinition
(ObjectContext的背景下,DbQueryCommandTree树)
在System.Data.Entity.Core.Objects.Internal。 ObjectQueryExecutionPlanFactory.Prepare(ObjectContext的背景下,DbQueryCommandTree树,类型元素类型,mergeOption mergeOption,布尔流,跨度跨度,IEnumerable`1 compiledQueryParameters,aliasGenerator aliasGenerator)
在System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan (Nullable`1 forMergeOption)
在System.Data.Entity.Core.Objects.ObjectQuery`1<> c__DisplayClassb< GetResults> b__a()$ b $在System.Data.Entity.Core b .Objects.ObjectContext.ExecuteInTransaction [T](Func`1 FUNC,IDbExecutionStrategy executionStrategy,布尔startLocalTransaction,布尔releaseConnectionOnSuccess)
在System.Data.Entity.Core.Objects.ObjectQuery`1<> c__DisplayClassb<。 ; GetResults> b__9()
在System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute [TResult](Func`1操作)
在System.Data.Entity.Core.Objects.ObjectQuery`1。 GetResults(Nullable`1 forMergeOption)LT
在System.Data.Entity.Core.Objects.ObjectQuery`1&;&System.Collections.Generic.IEnumerable LT; T> .GetEnumerator> b__0()
。在系统.Lazy`1.CreateValue()
在System.Lazy`1.LazyInitValue()在System.Lazy`1.get_Value
()
在System.Data.Entity.Internal.LazyEnumerator在System.Collections.Generic.List`1..ctor(IEnumerable`1集合)
在System.Linq.Enumerable.ToList [TSource](IEnumerable`1源)`1.MoveNext()

在MyProject.Infrastructure.DAL.Operations.UpdateUserinfo(的Int32 ID,型号Model)中:行168


解决方案

好吧,我面临着相同的问题,但发现解决方案,它可以帮助你。



在我的情况LanguageIDs集合类型是列表<长> (DB中unisgned INT),以及LanguageID是 INT 。而这种类型的不同而引起的问题。



我知道,它仍然是一个错误,而从观点.NET一点上,它是绝对正确的,但希望我的帖子将帮助别人


I have a strange issue, basically i have this code:

var langauges = (from l in context.languages 
                 where Model.LanguageIDs.Contains(l.LanguageID) 
                 select l)
                .ToList<language>();

I don't know why but I Always get an error:

System.NullReferenceException: Object reference not set to an instance of an object

The thing is it was working before, but after that i've upgraded all my application to MVC5 and EF6, it started to crash on this code.

Although if I check the Model.Langauges, it has several ID's so it is ok.

I also looped through tall the context.Languages and all of them have id's.

I also wrote like this:

var langauges = (from l in context.languages 
                 where 1==l.LanguageID 
                 select l)
                .ToList<language>();

And this WORKS also, so i really don't get it, what am i doing wrong?

Anyone can elaborate ...?

StackTrace:

   at MySql.Data.Entity.SqlGenerator.Visit(DbPropertyExpression expression)
   at MySql.Data.Entity.SqlGenerator.Visit(DbInExpression expression)
   at System.Data.Entity.Core.Common.CommandTrees.DbInExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)
   at MySql.Data.Entity.SqlGenerator.VisitBinaryExpression(DbExpression left, DbExpression right, String op)
   at MySql.Data.Entity.SqlGenerator.Visit(DbAndExpression expression)
   at System.Data.Entity.Core.Common.CommandTrees.DbAndExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)
   at MySql.Data.Entity.SelectGenerator.Visit(DbFilterExpression expression)
   at System.Data.Entity.Core.Common.CommandTrees.DbFilterExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)
   at MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type)
   at MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type)
   at MySql.Data.Entity.SelectGenerator.Visit(DbProjectExpression expression)
   at System.Data.Entity.Core.Common.CommandTrees.DbProjectExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)
   at MySql.Data.Entity.SelectGenerator.GenerateSQL(DbCommandTree tree)
   at MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
   at System.Data.Entity.Core.Common.DbProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree, DbInterceptionContext interceptionContext, IDbDependencyResolver resolver, BridgeDataReaderFactory bridgeDataReaderFactory, ColumnMapFactory columnMapFactory)
   at System.Data.Entity.Core.EntityClient.Internal.EntityProviderServices.CreateCommandDefinition(DbProviderFactory storeProviderFactory, DbCommandTree commandTree, DbInterceptionContext interceptionContext, IDbDependencyResolver resolver)
   at System.Data.Entity.Core.EntityClient.Internal.EntityProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.CreateCommandDefinition(ObjectContext context, DbQueryCommandTree tree)
   at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Boolean streaming, Span span, IEnumerable`1 compiledQueryParameters, AliasGenerator aliasGenerator)
   at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResults>b__a()
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResults>b__9()
   at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Lazy`1.get_Value()
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at MyProject.Infrastructure.DAL.Operations.UpdateUserinfo(Int32 id, Model Model) in :line 168

解决方案

Ok, i am faced exact same problem, but found solution which might help you.

In my case LanguageIDs collection type was List<long>(unisgned int in DB), and LanguageID was int. And this type difference caused problem.

I know, that it's still a bug, and from .net point of view it's absolutely correct, but hope my post will help somebody

这篇关于INT []。含有不EF6工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆