System.Data.SqlClient.SqlException: "Cannot insert the value NULL into colum..." (DateTime)
还没有人认领这个 Issue。
评估
调研方向
从 connection.Insert(history) 开始,将 Dapper.Contrib 生成的 insert 与可正常工作的 connection.Execute SQL 进行比较,并使用 CU_UnikatHistory 模型及其 Dapper.Contrib 特性作为输入。针对所示的 SQL Server 表重现该失败,并检查是否包含 TI_Stamp;完成标准是确认原因,并且已有一个聚焦的回归测试或已记录的解决方案。
由索引模型根据 Issue 内容生成。
描述
Hello,
not sure if this is an issue of Dapper, or Dapper.Contrib.
I tried to insert some data into an existing table via a self made REST API, but it always fails with this (partially German) exception:
Ausnahme ausgelöst: "System.Data.SqlClient.SqlException" in Dapper.dll
Eine Ausnahme vom Typ "System.Data.SqlClient.SqlException" ist in Dapper.dll aufgetreten, doch wurde diese im Benutzercode nicht verarbeitet.
Cannot insert the value NULL into column 'TI_STAMP', table '[..].CU_UNIKATHISTORY'; column does not allow nulls. INSERT fails.
The statement has been terminated.
The create statement for the table (table not invented by me):
CREATE TABLE [dbo].[CU_UNIKATHISTORY](
[UNIKATSTART] [numeric](12, 0) NOT NULL,
[UNIKATCOUNT] [numeric](12, 0) NULL,
[PRODUCT] [nvarchar](50) NULL,
[TI_STAMP] [datetime] NOT NULL,
[NOTES] [nvarchar](255) NULL,
CONSTRAINT [PK_CU_UNIKATHISTORY] PRIMARY KEY CLUSTERED
(
[TI_STAMP] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]
The Model:
using System;
using D=Dapper.Contrib.Extensions;
using System.ComponentModel.DataAnnotations;
namespace MesApi.Data.Model
{
[D.Table( "CU_UNIKATHISTORY" )]
public class CU_UnikatHistory
{
[Required]
public int? Unikatstart { get; set; }
public int? Unikatcount { get; set; }
[MaxLength( 50 )]
public string Product { get; set; }
[D.Key]
[Required]
public DateTime? TI_Stamp { get; set; }
[MaxLength(255)]
public string Notes { get; set; }
}
}
Also tried it with non-nullable variables - without success.
The code that fails:
public void WriteUnikatHistory( CU_UnikatHistory history )
{
using( var connection = new SqlConnection( csb.ConnectionString ) )
{
connection.Open();
connection.Insert( history ); //<--- System.Data.SqlClient.SqlException
}
}
A working oldschool solution without Dapper.Contrib:
public void WriteUnikatHistory( CU_UnikatHistory history )
{
var sql = "INSERT INTO [dbo].[CU_UNIKATHISTORY] ([UNIKATSTART],[UNIKATCOUNT],[PRODUCT],[TI_STAMP],[NOTES])"
+ " VALUES (@unikatstart, @unikatcount, @product, @ti_stamp, @notes);";
using( var connection = new SqlConnection( csb.ConnectionString ) )
{
connection.Open();
connection.Execute( sql, history );
}
}
Example data for the history-object in json format for insertion:
{
"Unikatstart": 1,
"Unikatcount": 2,
"Product": "string1",
"TI_Stamp": "2022-07-28T08:44:18.705Z",
"Notes": "string2"
}
As you might have noticed: I do supply a value for TI_Stamp.
When I debug the code, there is no null value being passed to connection.Insert():

Perhaps some conversion problems between .Net and SQL Datetime? Might it be related to #33?
Software used:
- Dapper: 2.0.123
- Dapper.Contrib: 2.0.78
- MS SQL: 14.0.3445.2
- Visual Studio 2022
- .NET Core 3.1
- Swashbuckle.AspNetCore: 6.4.0
- Swashbuckle.AspNetCore.Annotations: 6.4.0
- Swashbuckle.AspNetCore.SwaggerGen: 6.4.0
- 主要语言
- C#
- 星标
- 293
- 派生
- 108
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
DapperLib/Dapper.Contrib 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 75/100
DapperLib/Dapper.Contrib#22 · 2 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 42/100
DapperLib/Dapper.Contrib#174 ·
-
难度 2/5 1-3 小时 新手友好度 45/100
DapperLib/Dapper.Contrib#173 ·
-
难度 1/5 1 小时以内 新手友好度 10/100
DapperLib/Dapper.Contrib#172 · 1 条评论 · 4 个 reaction ·
-
难度 3/5 1-2 天 新手友好度 48/100
DapperLib/Dapper.Contrib#169 ·
查看 DapperLib/Dapper.Contrib 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 62/100
builtbybel/CrapFixer#112 ·
-
area-ai untriaged
难度 2/5 1-3 小时 新手友好度 85/100
dotnet/extensions#7790 ·
维护者通常 1 天内回复
-
Beginner Friendly T: Bugfix
难度 2/5 1-3 小时 新手友好度 82/100
space-wizards/space-station-14#46220 ·
维护者通常 1 天内回复
-
P2 testing
难度 1/5 1 小时以内 新手友好度 90/100
维护者通常 1 天内回复
-
area-Infrastructure-coreclr os-ios os-maccatalyst os-tvos untriaged
难度 2/5 1-3 小时 新手友好度 78/100
dotnet/runtime#134766 · 3 条评论 ·
维护者通常 1 天内回复