Confused about column type

.net, c#, entity-framework

Solution

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.

Problem

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.ErrorHandlingValueReader`1.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](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet) at lambda_method(Closure , Shaper ) at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 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.

Original source

Related problems