我如何配置实体框架来处理默认日期约束 [英] how do i configure entity framework to handle a default date constraint

查看:77
本文介绍了我如何配置实体框架来处理默认日期约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework 6,当我尝试创建一个新的记录时,我收到以下错误。但是,我在列上有一个默认值,所以当创建记录时,它从数据库中获取一个时间戳。


将datetime2数据类型转换为datetime数据类型
导致超出范围的值。该表已被终止。


在我的表中,我有一个具有默认约束的[created]列:

  [创建] [datetime] NOT NULL CONSTRAINT [DF_Notice_Created] DEFAULT(getdate()),
/ pre>

在我的班级,我有一个创建的财产:

  public System.DateTime Created {get;组; 

在我的Mapping类中,我有:

  this.Property(t => t.Created).HasColumnName(Created); 

我需要使用什么配置,让EF不尝试使用默认值创建列?

解决方案

尝试使用DateTime?作为您的类型(称为可空类型)


I am using Entity Framework 6, and when I try to create a new record I get the following error. However, I have a default value on the column, so that when the record is created, it gets a timestamp from the DB.

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.

In my table, I have a [created] column with a default constraint:

[Created] [datetime] NOT NULL CONSTRAINT [DF_Notice_Created]  DEFAULT (getdate()),

In my Class, I have a Created Property:

public System.DateTime Created { get; set; }

In my Mapping Class, I have:

this.Property(t => t.Created).HasColumnName("Created");

What configuration do I need to use, to have EF not try and create the column with a default value?

解决方案

Try using DateTime? as your type (it's called a nullable type)

这篇关于我如何配置实体框架来处理默认日期约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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