gpt4 book ai didi

c# - 为什么我的默认值列自动填充日期为 0001 年 1 月 1 日?

转载 作者:太空宇宙 更新时间:2023-11-03 11:39:15 25 4
gpt4 key购买 nike

表定义

enter image description here

模型定义

enter image description here

代码

using MyRandomizer.Models;

namespace MyRandomizer
{
class Program
{
static void Main(string[] args)
{
using (Database1Entities db = new Database1Entities())
{

Category c = new Category();
c.Name = "Integral";

db.Categories.AddObject(c);
db.SaveChanges();

}
}
}
}

输出

enter image description here

问题

为什么我的默认值列自动填充日期为 0001 年 1 月 1 日?

最佳答案

如果您在设计器中设置 StoreGeneratedPattern="Computed",它仅包含在概念模型 (CSDL) 中。但是您在存储模型 (SSDL) 中也需要这个。如果您不这样做,EF 将生成 INSERT 语句,其中包含日期的当前值,即 DataTime.MinValue。无法从设计器中编辑存储模型。您必须以 XML 格式打开 EDMX 文件并手动更新行定义。

最终定义应该是这样的:

<edmx:Edmx ...>
<edmx:Runtime>
<edmx:StorageModels>
<Schema ...>
<EntityContainer Name="...">
<EntitySet Name="Categories" EntityType="...Categories" store:Type="Tables" Schema="dbo" />
...
</EntityContainer>
<EntityType Name="Categories"/>
...
<!-- Here you must add StoreGeneratedPattern -->
<Property Name="CreationDate" Type="datetime" Nullable="false" StoreGeneratedPattern="Computed" />
</EntityType>
</Schema>
</edmx:StorageModels>
...
</edmx:Runtime>
</edmx:Edmx>

但是这个解决方案有一个大问题。手动修改 SSDL 部分后,不应使用“从数据库更新”,否则每次调用“从数据库更新”后都必须执行此更改。原因是“从数据库更新”删除了 EDMX 中当前的 SSDL 部分,并根据当前的数据库结构创建了新的部分。

关于c# - 为什么我的默认值列自动填充日期为 0001 年 1 月 1 日?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5216861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com