Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 4.0 Use Database Default Value

I am using Entity Framework 4.0

Many of our tables have a column called RowID, used by the DBA to track stuff. The column is NON NULLABLE has a database default value that maps to a function.

The problem is, the EDMX Model requires this property to have a value on the Entity, making construction of the object difficult.

I have read many posts on tweaking the EDMX and SSDL files manually to make this work. That is not feasible for our team.

Is there a better solution to this problem, other than making the column NULLABLE in the database? I don't care about getting the value back, I just want the database to be able to set it on its own.

like image 290
ctorx Avatar asked Jun 21 '10 21:06

ctorx


2 Answers

You should set the StoreGeneratedPattern = "Computed" directly in the SSDL (XML behind the Model).

1 - Right click the Model

2 - Select "Open With" => Xml Editor

3 - Look for the mapping of one of the Tables under the Model Content section

4 - Add the StoreGeneratedPattern = "Computed" attribute to the RowID column

5 - Do a global replace on your file so every table will have the RowID definition with the StoreGeneratedPattern="Computed"

NOTE: If you use the Update Model Wizard you will need to manually update the XML again. My recommendation: Don't use the Wizard or save the original and changed xml lines into a separate text file so that you have them available for do a global replace after running the wizard.

For more information refer to this article: http://msdn.microsoft.com/en-us/library/dd296755(v=vs.90).aspx

like image 166
Jazz Avatar answered Nov 16 '22 00:11

Jazz


If RowID never needs to be read or written in the context of your application then it doesn't belong in your Entity Model at all. Just remove it from your Entity Model using the Designer (Right Click > Delete) and let the database manage it.

like image 32
willbt Avatar answered Nov 16 '22 00:11

willbt