简体   繁体   中英

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.

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

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)

作为一种解决方法,我认为您可以使用数据库触发器将列值更新为默认值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM