1 2

c#事务处理(sqlTransaction)

事务:

 /// <summary>
    /// 删除考勤
    /// </summary>
    /// <param name="dto">Id</param>
    /// <returns></returns>
    public ResultEntity<bool> DeleteAttend(DelAttendEditorDTO dto)
    {
        //SqlConnection con = new SqlConnection(@"Data Source=.SQLEXPRESS;database=aaaa;uid=sa;pwd=jcx");
        //con.Open();
        using (var context = new HRModelsContainer())
        {
            var trans = context.Database.BeginTransaction();
            try
            {
                foreach (var Id in dto.Ids)
                {
                    //先删从表,再删主表
                    context.Database.ExecuteSqlCommand("DELETE FROM NoWorkDays WHERE ConfigId = @Id", new SqlParameter("@Id", Id));  
                    context.Database.ExecuteSqlCommand("DELETE FROM AttendancesConfig WHERE Id = @Id", new SqlParameter("@Id", Id));
                    context.SaveChanges();
                }
                trans.Commit();
            }
            catch (Exception error)
            {
                trans.Rollback();
                trans.Dispose();
                throw error;
            }
            finally
            {
                trans.Dispose();
            }
        }
        return GetResultEntity(true);
    }

 

posted @ 2018-09-10 11:50  大海的泡沫  阅读(1914)  评论(0编辑  收藏  举报
1 2