In Entity Framework, Default value constraints functionality is not been achieved, I tried it myself and then read about it here. So, I want to know what is the best way to get this. Should I use a stored procedure or trigger something like that? And which will be better or something new has been added in Entity Framework as I don't want to handle it explicitly in code. It will be a great help.
-
I have always just put the default value in the initialization of the entity. I know that is what you wanted to avoid but it works. – Matt Rowland Jan 16 '17 at 13:44
-
In my scenario, my entity framework gets updated more often and i have a number of tables so i usually avoid to make any change in these generated code files. – Wasim Bajwa Jan 16 '17 at 13:49
-
I'm not sure how you're generating your entity pocos (based on your comment above it sounds like you're doing database first) but most tools have the option to create partial classes for your entities. You could then create partial classes to perform any custom logic you need to perform when the entity is instantiated. Simon Hughes created one that works quite well - https://marketplace.visualstudio.com/items?itemName=SimonHughes.EntityFrameworkReversePOCOGenerator – Mike Devenney Jan 16 '17 at 14:41
-
1Thanks @MikeDevenney seems like a pretty good suggestion, i appreciate that. But In my case, database is been used by multiple applications each having their own Entity Framework model that's why i am looking for a more generic solution as i explained in later part of question. – Wasim Bajwa Jan 17 '17 at 06:19
3 Answers
In Database First approach, the entity framework ignores the default value constraints. However the Entity Framework displays Default Value property for columns.In Model.edmx viewer select an entity -> go the column -> properties -> Default Value. Add the default value for the column in it and that's it.
In my case, i updated the Model.edmx file through code. The code gets the default constraints from Db and then updates the conceptual model in Model.edmx file as i have large number of default value columns.
Default value constraint issue persists in all the versions of Entity Framework till EF6 and seems to be resolved in Entity Framework 7

- 395
- 3
- 14
-
-
@Wasim, you said you "update the Model.edmx file through code". How did you accomplish that? – awright Sep 29 '17 at 14:43
-
@Thomas, in my case database was not large enough to write a code script so i did it manually. – Wasim Bajwa Oct 13 '17 at 13:00
-
I have a default constraint and EF is not allowing to update it.. any help will be appriciated. – Beena Gupta Apr 15 '20 at 11:42
The easiest way is to initialize your property from C# and thus ensure the default value.
For Database First, the designer ignores your SQL default and you have to tell it to treat your column as Computed: select your entity -> go the column -> properties -> set StoreGeneratedPattern to Computed (it defaults to None)

- 22,016
- 16
- 145
- 164
-
Just want to ask is there any way to set this property for all the columns as it will be beneficial when there are number of tables. – Wasim Bajwa Jan 17 '17 at 13:15
-
@user7369792 - check [this question](http://stackoverflow.com/questions/12691854/setting-default-value-in-entity-framework-database-first) – Alexei - check Codidact Jan 17 '17 at 13:57
-
2The most important thing which i noticed late, it doesn't save the value if code updates it, only default value will be stored. Also tried the "Identity" option for StoreGeneratedPattern almost works in the same way however it only creates the default value while inserting and over this Entity Framework doesn't allow to update the value for "Identity" columns. – Wasim Bajwa Jan 18 '17 at 10:33
As a work around, I think you can use DB triggers to update column values to default.

- 546
- 1
- 7
- 16
-
-
I will definitely go to this option, if i can not get the solution from code side. – Wasim Bajwa Jan 20 '17 at 07:06