问题
I'm using Entityframework 6 with model first. In the diagram I have chosen decimal for my datatype. When I save decimal values, the database only has whole numbers and the decimals get dropped.
My first suspicion was to check the precision, however going to the properties window from the model first diagram designer, the precision and scale dropdowns have no other options besides "None".
How do I get EF to save my decimals? Is it a precision issue? If so, how do I set the precision in this case?
回答1:
OK I feel a little silly for not figuring this out earlier, but this is a simple fix.
In the properties window for the column in the edmx designer, both precision and scale are a drop down menu, however it still lets you type in it. So I just typed 2 in for scale, hit enter, and it worked.
Not sure why they have it setup to be a drop down if they don't provide you with any preset values and expect you to type it in.
回答2:
One other thing to note is that Entity Framework's "Update Model from Database" functionality can be very problematic. In my case I changed a few database fields from 2 decimal precision to 4 decimal precision, then did Update Model from Database, but it didn't modify the scale property when I looked at the field in the property window of this table in the .edmx diagram. So I manually changed the scale from 2 to 4 and tried again, but it was still saving data with 2 decimal places. A restart of Visual Studio still didn't work, so I tried Update Model from Database once more, and this time it did the trick.
This is not the first time that the database visual modeler in Visual Studio has given me grief. I prefer code-first to have control over everything, and this is just one of the reasons why. Complete and accurate updates to the model are never routine.
来源:https://stackoverflow.com/questions/24331751/entityframework-model-first-decimal-precision