如何在实体框架使用默认列的值与数据库? [英] How to use Default column value from DataBase in Entity Framework?

查看:255
本文介绍了如何在实体框架使用默认列的值与数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期列在表中有缺省值或绑定,GETUTCDATE()。我想用这个在实体framework.On产生电火花我能找到默认值属性栏的水平,但我认为这是很难codeD值。

I have a Date column in table which has default value or binding as getutcdate(). I want to use this in entity framework.On generating EDM I was able to find "Default Value " property at column level but I think it is for hardcoded value.

请让我知道我该如何使用在数据库中指定的默认​​值。

Please let me know how can I use default value specified in database.

谢谢, 麦

推荐答案

实施OnCreated事件实体是我已经找到了解决方案。我有一个GUID属性,我想填充。默认情况下它被填充全零(00000-0000-00000-等)。通过添加下面的实体是我部分类,我能够处理这一问题。

Implementing the OnCreated event for the entity is the solution I have found. I had a Guid property that I wanted to be populated. By default it was being populated with all zeros (00000-0000-00000-etc). By adding the following to my partial class of the entity I was able to deal with the issue.

partial void OnCreated()
{
    Guid = Guid.NewGuid();
}

这篇关于如何在实体框架使用默认列的值与数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆