如何使用LINQ

时间:2019-02-14 05:09:02

标签: c# asp.net linq

抛出错误“当 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();
}

0 个答案:

没有答案