问题
I have a project that I recently migrated from VS2010 to VS2012.
It contains an EF4.1 edmx file containing my entities. A few of these entities are based on views and are read-only. Although some columns returned by the views are nullable, I have changed the return value of these columns in the view to ensure that they do not return null by using
ISNULL(NumericColumnName, 0) AS NumericValueColumn
When updating the model via the "Update Model From Database" function in VS2010, the columns that were returned in this way were being created as non-nullable in my model. However, since the move to VS2012, this is no longer the case.
Now, whenever I use the "Update Model From Database" functionality, these properties are generated as nullable. What I have to do is manually set the columns to non-nullable. However, the entity properties will be regenerated as nullable the next time that I run "Update Model From Database". I then have to update them to non-nullable again.
Another work-around that I have tried is to move the view-based entities to their own edmx file. However, I am seeing the same behavior when updating either of the edmx files.
Another detail is that I am using Self-Tracking Entities, generated via the EF4.1 T4 STE templates.
Is there any way to prevent the VS2012's EF designer from overwriting my non-nullable attribute for the view-based entities?
EDIT
It turns out that the designer in VS2012 is trying to prevent you from shooting yourself in the foot, and consequently changes the member properties inside my entity.
When generating a member from the following:
SELECT ISNULL(Quantity,0) * ISNULL(Number, 0) AS Total FROM SomeTable
VS2012 will always set the member Total on the SomeTable entity to nullable because it does not understand that there is no way for the result of the multiplication to be 0. To prevent this behavior, change it to:
SELECT ISNULL(Quantity * Number, 0) AS Total FROM SomeTable.
Watch out for aggregations as ISNULL(SUM(SomeCol),0) is not the same as SUM(ISNULL(SomeCol,0)).
来源:https://stackoverflow.com/questions/12604004/how-to-preserve-non-nullable-setting-in-edmx-when-refreshing-model