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.
这篇关于实体框架创建记录时不插入数据库默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:实体框架创建记录时不插入数据库默认值
基础教程推荐
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
