Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 4 and Default Values

I might be missing something, but a SQL Server 2008 R2 database as generated by Entity Framework 4 is missing the default values I configured via the EF designer.

Any ideas what I could be doing wrong?

like image 842
Shaun Avatar asked Aug 27 '10 16:08

Shaun


1 Answers

The reason for that is because the default values that you can set in the conceptual model and the database defaults are completely unrelated.
Let's consider a typical situation where you already have a database that has default values set on columns, by the same token, that default will NOT get picked up in the store schema or by the entity itself.

Therefore, if you define a default value for a property of an entity in a model that will be used to generate a database schema, it’s important to be aware that none of the default values you define for an entity’s property will get pushed to the database.

However, it's worth mentioning that the default EntityObject T4 code generation template and the default POCO Entities template will both set that property’s default value in the generated class.

You can verify this by take a closer look at yourModelName.edmx.sql file that has been generated by VS2010 once you click on "Generate Database From Model...". As you can see, there is NO such a thing like this in it:
ADD CONSTRAINT DEFAULT "Default Value" FOR YourColumnName

like image 132
Morteza Manavi Avatar answered Oct 06 '22 02:10

Morteza Manavi