将datetime2数据类型转换为该值的smalldatetime数据类型超出范围

本文关键字:数据类型 smalldatetime 范围 数据 datetime2 类型转换 | 更新日期: 2023-09-27 18:11:16

我得到这个错误,因为我有datcreatefield作为smalldatetime作为空值在我的DB和EF抱怨它。这个链接

清楚地说明了可能的解决方案。

我想通过设置

来遵循第三条

DateCreated = DateTime.Now;

但是如果我在我的实体类上这样做,它将在下次更新或重新创建edmx文件后被覆盖。如果可能的话,我想在xaml上实现它。我有

这是我的xaml看起来像下面,我怎么做?还是有别的办法。

<DataGridTextColumn Header="DateCreated" IsReadOnly="True" Binding="{Path= myModel.DateCreated ,Mode=OneWay}" Width="130"></DataGridTextColumn>

将datetime2数据类型转换为该值的smalldatetime数据类型超出范围

但是如果我在实体类上这样做,它将在下次更新或重新创建edmx文件后被覆盖

这就是为什么实体框架生成部分类。用另一个部分创建一个新文件,并在其中添加构造函数:

public partial class YourEntitiy
{
    public YourEntitiy()
    {
        DateCreated = DateTime.Now;
    }
}