Waseem Aslam
Waseem Aslam

Reputation: 395

Entity Framework and Default Values

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.

Upvotes: 5

Views: 9044

Answers (3)

Waseem Aslam
Waseem Aslam

Reputation: 395

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

Upvotes: 9

Alexei - check Codidact
Alexei - check Codidact

Reputation: 23078

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)

Upvotes: 5

Behzad Qureshi
Behzad Qureshi

Reputation: 556

As a work around, I think you can use DB triggers to update column values to default.

Upvotes: 0

Related Questions