Reputation: 24959
I've been using EF5 db first for some time in a .net 4.5 web project, and it's working well.
I have one query that throws a null reference when calling Count() or ToList() or Any() etc.
lastQuery = dbContext.Set<T>();
logger.Info("last Query: {0}", lastQuery); // this shows the correct query
lastQuery.ToList(); // only when the query is executed, null reference is thrown
lastQuery.ToString() shows the correct db query.
this code works from localhost, dev, qa, and localhost pointing to uat/prod database.
entity framework 5 assembly (EntityFramework.dll 5.0.0.net45) is included in deployment and referenced locally.
It only breaks when this code is run on uat/prod server.
it was my suspicion that asp.net mvc 4 was not up to date, so i deployed a standalone install, but that didn't fix it.
looking for ideas on how else to troubleshoot it, or what components may be missing in prod etc..
call stack:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Data.EntityKey.ValidateTypeOfKeyValue(MetadataWorkspace workspace, EdmMember keyMember, Object keyValue, Boolean isArgumentException, String argumentName)
at System.Data.EntityKey.ValidateEntityKey(MetadataWorkspace workspace, EntitySet entitySet, Boolean isArgumentException, String argumentName)
at System.Data.Objects.ObjectStateManager.CheckKeyMatchesEntity(IEntityWrapper wrappedEntity, EntityKey entityKey, EntitySet entitySetForType, Boolean forAttach)
at System.Data.Objects.ObjectStateManager.AddEntry(IEntityWrapper wrappedObject, EntityKey passedKey, EntitySet entitySet, String argumentName, Boolean isAdded)
at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
at lambda_method(Closure , Shaper )
at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
Upvotes: 1
Views: 1272
Reputation: 46
This is a old thread, but I did cannot find the direct answer. Finally, I figure out by checking the code line by line.
In my case:
One of the column in view of db is nullable, but the view entity(inside file *.demx) which auto generated by EntityFramework IsNullable
was set false by exception. It work good after I modify the IsNullable
to true and removed it from key list manually. Alternatively, just delete the view model from the *.edmx and re-update it from the database.
// file: *.Designer.cs
[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
public global::System.String columnName
{
...
set
{
...
_columnName = StructuralObject.SetValidValue(value, false);
...
}
}
// file: *.demx
<Property Name="columnName" Type="varchar" MaxLength="50" Nullable="false" />
// file: *.Designer.cs
[EdmScalarPropertyAttribute(EntityKeyProperty=flase, IsNullable=true)]
public global::System.String columnName
{
...
set
{
...
_columnName = StructuralObject.SetValidValue(value, true);
...
}
}
// file: *.demx
<Property Name="columnName" Type="varchar" MaxLength="50" Nullable="true" />
It throw the NullReferenceException exception as value of one of the key column is NULL. Not match the column definition IsNullable=false
.
System.NullReferenceException: Object reference not set to an instance of an object.
This is the exception message to indicated that a NullReferenceException.
at System.Data.EntityKey.ValidateTypeOfKeyValue...
This line indicated that the exception final coming from the key value validation.
Upvotes: 0
Reputation: 24959
think i figured it out.
since this view is using db first, a primary key has to be defined.
in this case, there was no primary key column, so PK was composite of few fields:
modelBuilder.Entity<V_MVC_USER_ACTION_PERMISSION>().HasKey(u => new { u.LOGIN_NAME, u.CONTROLLER_NAME, u.ACTION_NAME, u.APPLICATION_NAME });
and in production a bunch of records were coming back with null action_name
this appears to be a dbcontext no no, and it will trip with a null context exception.
as a interim solution, i added a filter to my view
where Action_Name is not null
however, it would be nice to find a more comprehensive solution, where all such rows would be ignored instead of nulling the whole return value.
Upvotes: 2