'How do i insert data in a row based on whether a column is correct?
I want to be able to enter data to amount based on whether the social number is correct. I always get the error message SqlException: Cannot insert explicit value for identity column in table 'Customers' when IDENTITY_INSERT is set to OFF.. I have tried to make a sql command to the database and set Identity to ON, but I still get the same error. Is it possible to do what I want to do?
My Class
public class AddAmount : IAddAmount
{
private readonly ApplicationDbContext _db;
public AddAmount(ApplicationDbContext db)
{
_db = db;
}
public void Insert(int number)
{
Console.WriteLine("Add your amount you would like to insert: ");
try
{
var addAmount = _db.Customers.SingleOrDefault(x => x.SocialNumber == number);
addAmount.Amount = Convert.ToInt32(Console.ReadLine());
_db.Database.ExecuteSqlInterpolated($"SET IDENTITY_INSERT dbo.Customers ON");
_db.Add(addAmount);
_db.SaveChanges();
}
catch (Exception)
{
throw;
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
