Entity Framework is a great tool for quick and dirty data mappings and management. I encourage it for POCs and throw away pet projects, but will never even consider it for a production application. There are thousands of reasons for this; mainly they revolve around the scalability problems, performance issues with caching and lazy loading data and just plain fucked-up things that EF forces you to do in some cases, like break good design patterns. Here is a new one for me: EF does not support gracefully Non-Nullable columns with default values in the form of expressions. Let me break that down for you. Say you create a table in your schema with a column like this;

[sourcecode language="sql"][CreationDate] [datetime] NOT NULL DEFAULT (GetDate())[/sourcecode]

If you generate your edmx with Entity Framework and start adding records to your table, you’ll note at first your values look like this:

 

… and then you are like WTF! Yes, WTF is that Entity Framework does not support such thing as of the writing of this article (02/29/2012). Here is a post from Microsoft Connect explaining the problem:

http://connect.microsoft.com/VisualStudio/feedback/details/534037/entity-frameword-reports-an-error-if-i-dont-map-a-non-nullable-column-that-has-a-default-value

So what do you do?

Option 1: Don’t use Entity Framework

Option 2: Manually change the edmx XML by hand and every time you modify your schema you have to do it again. This option is kinda stupid, because I ONLY recommend the use of EF in for prototyping projects, which in turn change a lot, thus annulling the initial reason to use EF in this setup: SPEED. But let’s go with it.

In case you want to support such case, you need to open your edmx file in your favorite text editor as XML and search everywhere your field is referenced in the file like so:

[sourcecode language="xml"][/sourcecode]

And like

[sourcecode language="xml"]<ScalarProperty Name="CreateDate" ColumnName="CreateDate" />[/sourcecode]

And remove them. Just beware that next time around if you need to update your Entity Data Model because you changed your schema, you’ll have to go through this all over again.

I tried this Stack Overflow solution but didn't have much luck:

http://stackoverflow.com/questions/3076496/entity-framework-column-must-be-mapped-although-it-does-have-a-default-value

Comment