如何使用默认的实体框架和默认的日期值 [英] How to use the default Entity Framework and default date values
问题描述
在我的SQL Server数据库架构我有一个数据表,其中包含
In my SQL Server database schema I have a data table with a date field that contains a default value of
CONVERT(VARCHAR(10), GETDATE(), 111)
这是理想的自动插入日期到当手动输入到数据库中记录的新记录。
which is ideal for automatically inserting the date into the new record when manually entering records in to the database.
我是使用实体框架时和测绘日期字段,即实体框架插入一个默认的问题DateTime.Min价值当实体被实例化。
The problem I have is that when using the Entity Framework and mapping the Date field, that the Entity Framework inserts a default value of DateTime.Min when the entity is instantiated.
我不能日期字段映射到一个可空的DateTime,即日期时间?我也不是能够使用任何转换或DateTime.Now.Today在实体设计的默认值,因为它仅接受硬编码的常量。
I cannot map the Date field to a nullable DateTime, i.e. DateTime?, nor am I able to use either CONVERT or DateTime.Now.Today in the default value of the Entity Designer as it only accepts hard-coded constant values.
我当然可以明确地设置在C#每当DateTime字段该对象被明确地在代码中,在部分类的构造函数,甚至在保存更改事件创建的,无论是。
I can of course explicitly set the DateTime field in C# whenever the object is created, either explicitly in code, in the constructor of a partial class, or even during the saving changes event.
有没有实现任何其他方式我?想去的地方存放在数据库表中的默认值计算实际上是用来代替
Are there any other ways of achieving what I want where the default value calculation stored in the database table is actually used instead?
推荐答案
我只是碰到了这一点 - 我工作围绕它通过使用部分方法设定在实体的构造函数的日期(我需要汽车领域产生)。这是否是理想的还是它在所有情况下,仍有待观察,但到目前为止,它已经固定我的问题。
I just ran into this - I worked around it by setting the date (the field I needed auto generated) in the constructor of the Entity using a partial method. Whether this is Ideal or whether it works in all cases is yet to be seen, but it has fixed my problem so far.
这篇关于如何使用默认的实体框架和默认的日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!