3

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
Alnedru
  • 2,573
  • 9
  • 50
  • 88
  • `where 1==l.LanguageID` ? – gunr2171 Jan 09 '14 at 19:15
  • yes 1 or 3 or 4 ==l.LanguageID it works, so it only doesn't work with int[].contains :( – Alnedru Jan 09 '14 at 19:17
  • 1
    I'd guess `Model.LanguageIDs` is `null`. – Cory Nelson Jan 09 '14 at 19:18
  • You said there are several languages in `Model.Languages`, but in your query you're using `Model.LanguageIDs`. – MarcinJuraszek Jan 09 '14 at 19:18
  • I think ,might be simple Contains work else than model.lan.bla bla – Charlie Jan 09 '14 at 19:19
  • but the thing is before it was working fine, and normally if i have to int[] i can use contains ... and then why it gives me a null reference exception and not something else :/ What can the other possibliity be to use something else except contains? – Alnedru Jan 09 '14 at 19:24
  • Posting the stack trace of the exception might help. – Slauma Jan 09 '14 at 19:25
  • ok, one sec, ill post the stack trace – Alnedru Jan 09 '14 at 19:28
  • You seem to use a MySql provider for EF (according to stack trace). Did you check that it officially supports EF 6? (Or if there is an update for the provider that supports EF 6?) – Slauma Jan 09 '14 at 19:34
  • yes, it supports from 6.8.x, and i have 6.8.3 and the thing is i can update everything else except the languages .... and i retrieve the information – Alnedru Jan 09 '14 at 19:43
  • Well, the null ref exception is deep inside MySql provider code which, I think, indicates that this is a bug. I'd try to reproduce the problem with a small test model and the same query using `Contains` and if the problem occurs again I'd report this at MySql forum/support. – Slauma Jan 09 '14 at 20:04
  • ok, ill try, to do that, meanwhile what would be the workaround for me? No the foreach i hope? ) – Alnedru Jan 09 '14 at 20:12
  • I think I'd try `context.Language.SqlQuery(...)...` as workaround for now and build a SQL query string as parameter, probably with an `IN (1,2,3,...)` clause. – Slauma Jan 09 '14 at 20:20
  • Looks like a bug in MySql provider. Consider filing it. – Pawel Jan 09 '14 at 21:50

2 Answers2

0

Is it possible that Model.LanguageIDs is null or your context has not yet been setup?

Also, do you allow for null language entries in context.languages? If anyone of those is null, you'll get this exception.

poy
  • 10,063
  • 9
  • 49
  • 74
  • context.languages there is nothing null, all have primary keys etc ... i just did a seperate loop on them and got the ID's. Model.LanguageIDs is also not null i checked it in debug mode – Alnedru Jan 09 '14 at 19:21
  • 1
    This is more a comment than an answer, because you are asking clarification questions and taking stabs in the dark. – gunr2171 Jan 09 '14 at 19:21
0

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

Uriil
  • 11,948
  • 11
  • 47
  • 68