You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
I think you basically have same problem as I do here: #141
Key columns are always excluded from list of updateable/insertable fields.
And you can't remove [Key] from TI_Stamp field because otherwise you'll get "Entity must have at least one [Key] or [ExplicitKey] property..." exception.
As far as I can tell problem is not related to any specific .NET framework version, nor type of SQL server/driver in use, and appears to be present for quite some time already.
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:
The create statement for the table (table not invented by me):
The Model:
Also tried it with non-nullable variables - without success.
The code that fails:
A working oldschool solution without Dapper.Contrib:
Example data for the history-object in json format for insertion:
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:
The text was updated successfully, but these errors were encountered: