Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

System.Data.SqlClient.SqlException: "Cannot insert the value NULL into colum..." (DateTime)

未关闭
#142 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
停滞
技术栈
csharp, sql
领域
backend, databases

调研方向

从 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():
dapper contrib

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,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

DapperLib/Dapper.Contrib 的其他 Issue

查看 DapperLib/Dapper.Contrib 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。