Hi, I'm new to Entity Framework .Net Core, I'm receiving an error after update of a record.
SqlException: Cannot insert explicit value for identity column in table 'tblAllTransactionsWithDetails' when IDENTITY_INSERT is set to OFF.
I don't know how to get past this one. I set up my table model with a primary [Key]
public class tblAllTransactionsWithDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int internalID { get; set; }
}
In my Connection Strings override I have the following:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TransactionsManualTempTable>()
.ToTable("TransactionsManualTempTable").ToString();
modelBuilder.Entity<tblSpreadsheetInput>()
.HasNoKey();
modelBuilder.Entity<tblAllTransactionsWithDetails>()
.Property(p => p.internalID)
.ValueGeneratedOnAdd();
}
In my controller I have
_context.tblAllTransactionsWithDetails.Add(tblAllTransactionsWithDetails);
try
{
_context.tblAllTransactionsWithDetails.FromSqlRaw("SET IDENTITY_INSERT dbo.tblAllTransactionsWithDetails ON;");
await _context.SaveChangesAsync();
}
It errors on the
SaveChangesAsync
with the Identity after trying to add a Second record in a row.
I have an AddNewRecord Page, then on click of the button, I want to add/save the record and reload the page, then let the user add another record. It's on the Second record that it errors out.
Thanks,
Heather
What I have tried:
I have tried:
_context.tblAllTransactionsWithDetails.FromSqlRaw("SET IDENTITY_INSERT dbo.tblAllTransactionsWithDetails ON;");
But this doesn't seem to work.