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


- 如何用自己压缩一个 IEnumerable 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04