实体框架创建记录时不插入数据库默认值

Database default value is not inserted while create record by entity framework(实体框架创建记录时不插入数据库默认值)

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

问题描述

我在 sqlserver 2008 中有一个 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 文件中日期时间的StoredGeneratedPattern"属性设置为已计算".来自以下线程:http://www.stackoverflow.com/a/4688135/2488939

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

为此,请通过单击您的 edmx 文件转到 edmx 文件设计器.然后找到您的桌子和房产.右键单击表中要更改的列,然后单击属性.然后应该出现属性窗口,您将看到属性StoredGeneratedPattern"之一.将其更改为计算.

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 "StoredGeneratedPattern". Change that to computed.

这篇关于实体框架创建记录时不插入数据库默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:实体框架创建记录时不插入数据库默认值

基础教程推荐