2014-01-09 54 views
3

不工作,我有一個奇怪的問題,基本上我有這樣的代碼:INT []包含在EF6

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

我不知道爲什麼,但我總是得到一個錯誤:

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

事情是它工作之前,但之後,我已經升級我的所有應用程序到MVC5和EF6,它開始崩潰在此代碼。

雖然如果我檢查Model.Langauges,它有幾個ID,所以它沒問題。

我還穿過高高的context.Languages和他們都有id的。

我也寫過這樣的:

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

而這個作品也,所以我真的不明白,我究竟做錯了什麼?

任何人都可以詳細說明......?

堆棧跟蹤:

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 
+0

'其中1 == l.LanguageID'? – gunr2171

+0

是1或3或4 == l.LanguageID它的工作原理,所以它只適用於int []。contains :( – Alnedru

+1

我猜'Model.LanguageIDs'是'null'。 –

回答

0

難道Model.LanguageIDsnull或您context至今尚未建立?

此外,您是否允許nulllanguage條目context.languages?如果其中任何一個是null,你會得到這個例外。

+0

context.languages沒有任何null,都有主鍵等...我只是做了一個單獨的循環,並得到了ID。 Model.LanguageIDs也不爲空,我在調試模式下檢查它 – Alnedru

+1

這是一個比答案更多的評論,因爲你要求澄清問題並在黑暗中進行刺探。 – gunr2171

0

好吧,我面臨完全相同的問題,但找到了解決方案,可能會幫助你。

在我的情況下,LanguageID收集類型爲List<long>(DB中的unisgned int),而LanguageID爲int。而這種差異造成了問題。

我知道,它仍然是一個錯誤,而且從.NET點是絕對正確的,但希望我的帖子將幫助別人