为什么我的默认值列自动填充日期 0001 年 1 月 1 日?
表定义
模型定义
代码
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();
}
}
}
}
输出
问题
为什么我的默认值列会自动填充截止日期为 0001 年 1 月 1 日?
Table Definition
Model Definition
Code
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();
}
}
}
}
Output
Question
Why does my default value column automatically get populated by a date of January 1, 0001?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在设计器中设置
StoreGeneratePattern="Compulated"
,则它仅包含在概念模型 (CSDL) 中。但您在存储模型 (SSDL) 中也需要它。如果不这样做,EF 将生成 INSERT 语句,其中包含日期的当前值,即DataTime.MinValue
。设计器无法编辑存储模型。您必须以 XML 格式打开 EDMX 文件并手动更新行定义。最终的定义应该是这样的:
但是这个解决方案有一个大问题。一旦您手动修改 SSDL 部分,您就不应该使用“从数据库更新”,否则您必须在每次调用“从数据库更新”后执行此更改。原因是“从数据库更新”删除了 EDMX 的当前 SSDL 部分,并根据当前数据库结构创建新的 SSDL 部分。
If you set
StoreGeneratedPattern="Computed"
in designer it is only included in conceptual model (CSDL). But you also need this in storage model (SSDL). If you don't do it EF will generate INSERT statement which will contain current value of date which isDataTime.MinValue
. Storage model cannot be edited from designer. You must open EDMX file as XML and manually update row definition.Final definition should look like:
But this solution has one big problem. Once you manually modify SSDL part you should not use "Update from database" or you will have to perform this change after each call of "Update from database". The reason is that "Update from database" deletes current SSDL part of EDMX and creates new one based on current DB structure.
看图片模型定义。未指定默认值。您的数据库实体不知道默认值。因此,它使用 CreationDate 的当前值。尝试将模型定义的默认值设置为合理的值。
Look at the picture Model Definition. The default value is not specified. You database entity doesn't know that the default value. So, it uses the current value of CreationDate. Try to set the default value for your model definition to something sensible.