I work on entity framework 7 asp.net core razor page . I get exception error when insert
data into ZebraSubPrinters from entity framework to SQL server table
I get exception error SqlException: Cannot insert explicit value for identity column in table 'ZebraSubPrinters' when IDENTITY_INSERT is set to OFF.
table ZebraSubPrinters have ID as identity column
public class ZebraSubPrinters
{
public decimal ID { get; set; }
public string BranchCode { get; set; }
public string DisplayName { get; set; }
public string Category { get; set; }
public string PrinterName { get; set; }
public string PrinterIP { get; set; }
public bool IsActive { get; set; } = true;
}
generic repository used for insert and save data
public virtual TEntity Insert(TEntity entity)
{
var result = dbSet.AddAsync(entity).Result.Entity;
return result;
}
public void Save()
{
try
{
_basecontext.SaveChanges();
}
catch (Exception ex)
{
throw;
}
}
Function that do insert and save using generic repository :
public void InsertZebraSubPrinters(ZebraSubPrinters printer)
{
ZebraSubPrinters ZEB = new ZebraSubPrinters();
ZEB.IsActive = printer.IsActive;
ZEB.PrinterName = printer.PrinterName;
ZEB.PrinterIP = printer.PrinterIP;
ZEB.Category = printer.Category;
ZEB.DisplayName = printer.DisplayName;
ZEB.BranchCode = printer.BranchCode;
aDCSupportUnitOfWork.ZebraSubPrinters.Insert(ZEB);
aDCSupportUnitOfWork.ZebraSubPrinters.Save();
}
SQL profiler generated error for insert statement above as below:
exec sp_executesql N'SET IMPLICIT_TRANSACTIONS OFF;
SET NOCOUNT ON;
INSERT INTO [ZebraSubPrinters] ([ID], [BranchCode], [Category], [CreatedBy], [CreatedDate], [DisplayName], [IsActive], [PrinterIP], [PrinterName], [SavedDate], [UpdatedBy], [UpdatedDate])
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11);
',N'@p0 decimal(18,2),@p1 nvarchar(4000),@p2 nvarchar(4000),@p3 nvarchar(4000),@p4 datetime2(7),@p5 nvarchar(4000),@p6 bit,@p7 nvarchar(4000),@p8 nvarchar(4000),@p9 datetime2(7),@p10 nvarchar(4000),@p11 datetime2(7)',@p0=0,@p1=N'10207',@p2=N'Green',@p3=NULL,@p4='0001-01-01 00:00:00',@p5=N'TANTAWY',@p6=1,@p7=NULL,@p8=NULL,@p9='0001-01-01 00:00:00',@p10=NULL,@p11='0001-01-01 00:00:00'
How to solve and prevent this error from happen when inserting?
Naimish MakwanaPosted Apr 2, 2024, 4:27 AM
You’re not explicitly setting the
IDin yourInsertZebraSubPrintersmethod. However, the issue might be arising due to the way Entity Framework handles entities. When you create a new instance ofZebraSubPrinters, theIDproperty is automatically initialized to its default value, which is0for a decimal type. When you try to save this entity, Entity Framework might be trying to insert0into theIDcolumn, which is not allowed because it’s an identity column.To resolve this, you can try making the
IDproperty nullable in yourZebraSubPrintersclass:By making
IDnullable, when you create a newZebraSubPrintersinstance without settingID, its value will benullinstead of0. When Entity Framework seesnull, it knows not to includeIDin theINSERTstatement, and SQL Server will automatically generate a value for it.Thanks
Jaimin ShethiyaPosted Apr 2, 2024, 4:09 AM
Hello,
You need to manually assign the ID value.
Thanks
Tahir AnsariPosted Aug 20, 2023, 6:37 AM
this occurs because you're trying to explicitly insert a value into the identity column
IDof theZebraSubPrinterstable. In your code, theInsertZebraSubPrintersfunction is creating a new instance ofZebraSubPrintersand setting various properties, including theIDproperty.Since the
IDcolumn is defined as an identity column, SQL Server automatically generates its value when you insert a new row. You should not provide a value for theIDcolumn during insertion.Tahir AnsariPosted Aug 20, 2023, 6:34 AM
SET IDENTITY_INSERT ZebraSubPrinters ON