4

I have an entity framework model generated from database. One of the entities is 'Session' with 'Type' int property.

Auto-generated class:

public class Session
{
    int Type { get; set;}    
}

Edmx:

<EntityType Name="Sessions">
      <Property Name="Type" Type="int" Nullable="false" />
</EntityType>

Sometimes when loading database values, I get an exception saying that it cannot set the 'Type' property (which is int) to a 'string' value:

System.InvalidOperationException: The 'Type' property on 'Session' could not be set to a 'System.String' value. You must set this property to a non-null value of type 'System.Int32'. at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader1.GetValue(DbDataReader reader, Int32 ordinal) at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName) at lambda_method(Closure , Shaper ) at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet) at lambda_method(Closure , Shaper ) at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator1.ReadNextElement(Shaper shaper) at System.Data.Entity.Core.Common.Internal.Materialization.Shaper1.SimpleEnumerator.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable1 source)

This is the query where it fails:

var session = db.Sessions.Include("Game.Mod").Where(s => s.UniqueId == message.SessionUid && s.DomainId == this.DomainId && s.Game.UniqueId == message.GameUid).FirstOrDefault();

When debugging locally, all is ok. This is when deployed to prod.

I'm currently using EF 6.1 on sql azure and I think it might be something to do with the upgrade, I don't think this was happening before (using 6.1). But I might be wrong.

Database column is also an int (100% confirmed), the mapping is correct.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1275154
  • 1,120
  • 1
  • 12
  • 24
  • are your Db-s in sync? As you mentioned prod - it seems as if your prod db is targeting earlier code, and has some strings in place where you have ints now. The 'materialization' error suggests it's unable to 'materialize' db field value into an object property. At least, looks like that. Also, issues like that may appear if/when reconfiguring the model, relationships, but doesn't seem to be the case here – NSGaga-mostly-inactive Apr 14 '14 at 23:46
  • The prod db column is also an int, just checked. The model has been updated, adding additional columns in latest release. However that should not affect existing 'Type' column, which has not changed. Keep in mind that I've run this locally against prod DB and it works fine – user1275154 Apr 14 '14 at 23:54
  • I know, but that 'smells' of synchronization issue - migrating and keeping it all in sync isn't as simple with EF - you have 'migration' tables, your code and the Db, 3 things really (not sure if latest EF version changed something, but doubt it). Here is one post of mine about it FYI [how to sync](http://stackoverflow.com/questions/10254613/mvc3-and-code-first-migrations/10255051#10255051) – NSGaga-mostly-inactive Apr 15 '14 at 00:00
  • I'm not using Code First, there is no migration. The model is generated from database. – user1275154 Apr 15 '14 at 00:17
  • Does the database column allow nulls by any chance? If so your model property must be defined as `int?` I'm just keying in on the "set this property to a non-null value" part of the exception. – Craig W. Apr 15 '14 at 01:49
  • Looks like you Store (SSDl) and Conceptual (CSDL) models are not in sync. Try validating your database from Model Editor. – Andrew Apr 15 '14 at 12:21
  • Validation Completed without any errors. The column is set as Not Null. – user1275154 Jul 07 '14 at 22:19
  • Do any other queries exhibit issues with execution that have the same data types? If you clean your solution, does it still work locally? – Justin Jul 09 '14 at 13:29
  • Did you find a fix for your problem? I am experiencing the same issue. It first occurred during a load test of my REST-API. It does not happen every time, but from time to time, EF throws these exceptions. 90% of the time everything is fine... Strange. – Olav Haugen Jul 15 '14 at 12:45

1 Answers1

5

First part of the error message

System.InvalidOperationException: The 'Type' property on 'Session' could not be set to a 'System.String' value.

The first sentence implies that a conversion is attempted at some point from int to string. Your database field and property are ints though. I believe you but a conversion is attempted by EF.

Try saving the query to a variable and execute the FirstOrDefault() on a different line.

var query = db.Sessions.Include("Game.Mod").Where(s => s.UniqueId == message.SessionUid && s.DomainId == this.DomainId && s.Game.UniqueId == message.GameUid);
var session = query.FirstOrDefault();

Then try saving the SQL generated before executing query in the database. This might give you a clue.

Second part of the error message

You must set this to a non-null value of type 'System.Int32'

To solve your problem, .Net is suggesting that you set the variable to a non-null value. This is because you are not allowed null values in the database...

Have you tried setting the field as nullable?

Don't forget to check you do the change in database as well as in the code.


Additional insight "possibly" useful

Also, I am tempted to risk saying that you're facing the problem in production but not locally because you have different data. Don't forget that the runtime exception happens when trying to do something with data.

Fabio Milheiro
  • 8,100
  • 17
  • 57
  • 96