我有一个5列的数据表,其中一行被数据填充,然后通过事务保存到数据库。
保存时,返回一个错误:
将datetime2数据类型转换为datetime数据类型会导致值超出范围
它暗示,正如阅读,我的数据表有一个类型的DateTime2和我的数据库一个DateTime;这是错误的。
date列设置为DateTime,如下所示:
新数据专栏(“myDate, Type.GetType”)
问题
是否可以在代码中解决这个问题,或者是否必须在数据库级别上更改某些内容?
我有一个5列的数据表,其中一行被数据填充,然后通过事务保存到数据库。
保存时,返回一个错误:
将datetime2数据类型转换为datetime数据类型会导致值超出范围
它暗示,正如阅读,我的数据表有一个类型的DateTime2和我的数据库一个DateTime;这是错误的。
date列设置为DateTime,如下所示:
新数据专栏(“myDate, Type.GetType”)
问题
是否可以在代码中解决这个问题,或者是否必须在数据库级别上更改某些内容?
当前回答
当我想用ASP编辑一个页面时,我看到了这个错误。净MVC。我没有问题,而创建,但更新数据库使我的DateCreated属性超出范围!
当您不希望您的DateTime属性为空,并不想检查它的值是否在sql DateTime范围(和@Html。HiddenFor没有帮助!),只需在相关类(控制器)中添加一个静态DateTime字段,并在GET操作时给它一个值,然后在POST执行它的工作时使用它:
public class PagesController : Controller
{
static DateTime dateTimeField;
UnitOfWork db = new UnitOfWork();
// GET:
public ActionResult Edit(int? id)
{
Page page = db.pageRepository.GetById(id);
dateTimeField = page.DateCreated;
return View(page);
}
// POST:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Page page)
{
page.DateCreated = dateTimeField;
db.pageRepository.Update(page);
db.Save();
return RedirectToAction("Index");
}
}
其他回答
您将有日期列,它被设置为允许的数据时间的最小值,如1/1/1001。
为了克服这个问题,你可以为ur属性设置正确的datetime值,也可以设置另一个神奇的属性,如IsSpecified=true。
看看下面两个: 1)该字段没有NULL值。例如:
public DateTime MyDate { get; set; }
替换:
public DateTime MyDate { get; set; }=DateTime.Now;
2)重新创建数据库。例如:
db=new MyDb();
创建一个基于@sky-dev实现的基类。所以这可以很容易地应用到多个上下文和实体中。
public abstract class BaseDbContext<TEntity> : DbContext where TEntity : class
{
public BaseDbContext(string connectionString)
: base(connectionString)
{
}
public override int SaveChanges()
{
UpdateDates();
return base.SaveChanges();
}
private void UpdateDates()
{
foreach (var change in ChangeTracker.Entries<TEntity>())
{
var values = change.CurrentValues;
foreach (var name in values.PropertyNames)
{
var value = values[name];
if (value is DateTime)
{
var date = (DateTime)value;
if (date < SqlDateTime.MinValue.Value)
{
values[name] = SqlDateTime.MinValue.Value;
}
else if (date > SqlDateTime.MaxValue.Value)
{
values[name] = SqlDateTime.MaxValue.Value;
}
}
}
}
}
}
用法:
public class MyContext: BaseDbContext<MyEntities>
{
/// <summary>
/// Initializes a new instance of the <see cref="MyContext"/> class.
/// </summary>
public MyContext()
: base("name=MyConnectionString")
{
}
/// <summary>
/// Initializes a new instance of the <see cref="MyContext"/> class.
/// </summary>
/// <param name="connectionString">The connection string.</param>
public MyContext(string connectionString)
: base(connectionString)
{
}
//DBcontext class body here (methods, overrides, etc.)
}
简短的回答
如果没有初始化DateTime字段的值,就会发生这种情况;该字段不接受NULL值,并且它是一个值类型,因此将使用不可空的DateTime类型的默认值。
设置值固定它为我!
长回答
默认值(DateTime)为DateTime。MinValue(或新的DateTime(1,1,1)或01/01/0001),这不是一个有效的SQL日期时间值。
由于使用的是公历,SQL Server datetime的最低有效值是01/01/1753。但是SQL Server DateTime2支持从01/01/0001开始的日期。实体框架默认使用DateTime2来表示日期,因此生成的SQL隐式地将生成的DateTime2值强制转换为SQL服务器端的DateTime值。
已经有很多好的答案了。我想展示一种使用实体框架core fluent api来解决这个问题的方法。 大多数情况下,错误发生在默认情况下生成DateTime值时,因此您需要覆盖DataContext中的OnModelCreating,并在您的特定实体的属性中手动添加默认值,以便EF能够正确地转换到数据库,如下所示:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>(entity =>
{
entity.ToTable("MyTable");
entity.Property(e => e.MyColumnDateTime)
.HasDefaultValueSql("GETDATE()");
});
OnModelCreatingPartial(modelBuilder);
}