The Problem
You are coding happily away in Visual Studio 2010, working on your asp.net project, using the entity framework and sql server and you get and you get the following error
The ‘Amount’ property on ‘SomeTable’ could not be set to a ‘Decimal’ value. You must set this property to a non-null value of type ‘Double’.
What you say? Why do I have to set it to non-nullable? It looks something like
public double? Amount { get; set; }
The Cause
The root problem is in your database schema – something in how that table is set up is not gibing correctly with entity framework.
The Solution
Just set the property to a non-nullable decimal type, for example
public decimal Amount { get; set; }
It’s not a perfect solution, but Entity Framework does not like all sql server data types, and I have yet to find an explanation as to why or a list of things to avoid. If anyone does know please leave a link in the comments. This is a persistent problem for me when working with ourside databases, and I’m sure I’m not the only this happens to. Your input would be quite helpful here Microsoft. It would be a nice improvement in the next version of Entity Framework.
|
Written By Steve French |
I had the same pain.
I have an int in my model with a MSSQL DB that has valid null for some of the rows.
I changed int to int? (ie int followed by a question mark)
I can now continue and find other pains
Same problem for me but with types .NET Guid and SQL uniqueidentifier:
The ‘x’ property on ‘y’ could not be set to a ‘System.Guid’ value. You must set this property to a non-null value of type ‘System.String’.
The problem happen when the column ‘x’ in DB is set as non nullable.