数据库的默认值不插入,同时通过实体框架创造纪录 [英] Database default value is not inserted while create record by entity framework

查看:112
本文介绍了数据库的默认值不插入,同时通过实体框架创造纪录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在2008年SQLSERVER具有以下柱结构LoginRecord表 -

I have a LoginRecord table in sqlserver 2008 with the following column structure-

LoginId      - int, identity
UserId       - int
LoginDateTime- Allow nulls false,default value getdate()

我插入通过下面的实体框架6的新纪录 -

I am inserting new record by entity framework 6 as below-

 db.LoginRecords.Add(new LoginRecord() { UserId = UserId }); 
 db.SaveChanges();



但在LoginDateTime表,被插入空值。这应该是当前日期时间。

But in LoginDateTime table, null value is being inserted. It supposed to be current datetime.

我使用的数据库的第一种方法。

I am using database first approach.

如何解决这个问题?

推荐答案

结合我的两个意见到一个答案。

Combined my two comments into an answer.

尝试设置您的日期时间在EDMX了StoredGeneratedProperty属性的文件来计算的。从下列线: http://www.stackoverflow.com/a/4688135/2488939

Try setting the "StoredGeneratedProperty" attribute of your datetime in the EDMX file to Computed. From the following thread: http://www.stackoverflow.com/a/4688135/2488939

要做到这一点,转到EDMX文件设计师通过点击您的EDMX文件。然后找到你的表和财产。在你想改变,然后点击属性表中右键单击列。然后,属性窗口应该拿出你将看到属性StoredGeneratedProperty之一。更改为计算出来的。

To do this, go to the edmx file designer by clicking on your edmx file. Then locate your table and the property. Right-click the column in the table that you want to change and click on properties. The property window should then come up and you will see as one of the properties "StoredGeneratedProperty". Change that to computed.

这篇关于数据库的默认值不插入,同时通过实体框架创造纪录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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