10

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?

Shaun
  • 706
  • 6
  • 11

1 Answers1

12

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

Morteza Manavi
  • 33,026
  • 6
  • 100
  • 83
  • Thank you for the concise answer. Makes perfect sense. – Shaun Aug 27 '10 at 19:17
  • It does make sense although makes code migrations harder. Add a non-nullable field to an existing table, the generated migration script from comparing the two schemas in SQL Studio won't work. Has to be done manually again. – simbolo May 23 '13 at 16:52