抛出错误“当 IDENTITY_INSERT设置为OFF 时,无法在表'Employee'中为身份列插入显式值”。所以任何人都可以向我建议我做错了什么。
public async Task<int> InsertEmployeeDetails(EmployeeDetails value){
SqlConnection conn = new SqlConnection(Startup.ConnectionString);
try
{
conn.Open();
SqlCommand command = new SqlCommand("SET IDENTITY_INSERT Employee ON");
command.Connection = conn;
command.ExecuteNonQuery();
InsertEmployeeDetails(value);
}
catch(Exception e)
{
throw e;
}
finally
{
SqlCommand comm = new SqlCommand("SET IDENTITY_INSERT Employee OFF");
comm.Connection = conn;
comm.ExecuteNonQuery();
conn.Close();
}
return await _context.SaveChangesAsync();
}
public void InsertEmployeeDetails(EmployeeDetails value){
EmployeeDetails employeeDetails=new EmployeeDetails();
employeeDetails.Name=value.Name;
employeeDetails.Description=value.Description;
employeeDetails.Designation=value.Designation;
_context.Employee.Add(employeeDetails);
_context.SaveChanges();
}