如何在Entity Framework中使用DataBase中的默认列值? [英] How to use Default column value from DataBase in Entity Framework?
问题描述
我在表中有一个Date列,它具有默认值或绑定为getutcdate()。我想在实体框架中使用。在生成EDM时,我能够在列级别找到默认值属性,但我认为这是硬编码的值。
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-etc)。通过将以下内容添加到我能够处理该问题的实体的部分类中。
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();
}
这篇关于如何在Entity Framework中使用DataBase中的默认列值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!