public int CreateUser(T_USER entity, T_USER_ROLE_RELATION rolemodel) { using (var dbContext = new DuPont_TestContext()) { //创建事务,如果操作过程中出现异常,操作会回滚,防止产生脏数据 using (var tran = dbContext.Database.BeginTransaction()) { dbContext.T_USER.Add(entity); dbContext.SaveChanges(); rolemodel.MemberType = true; rolemodel.UserID = entity.Id; dbContext.T_USER_ROLE_RELATION.Add(rolemodel); dbContext.SaveChanges(); //如果是经销商 if (rolemodel.RoleID == (int)RoleType.Dealer) { string controlAreaLevel = ""; if (!string.IsNullOrEmpty(entity.Village) && entity.Village != "0") { controlAreaLevel = entity.Village; } else if (!string.IsNullOrEmpty(entity.Region) && entity.Region != "0") { controlAreaLevel = entity.Region; } else if (!string.IsNullOrEmpty(entity.City) && entity.City != "0") { controlAreaLevel = entity.City; } else if (!string.IsNullOrEmpty(entity.Province) && entity.Province != "0") { controlAreaLevel = entity.Province; } else { throw new Exception("经销商的地址不能为空!"); } var supplier_area = new T_SUPPLIERS_AREA { AID = controlAreaLevel, CreateDateTime = DateTime.Now, State = true, UserID = entity.Id }; var paras = new SqlParameter[] { new SqlParameter("@aid", supplier_area.AID), new SqlParameter("@createdatetime", supplier_area.CreateDateTime), new SqlParameter("@state", supplier_area.State), new SqlParameter("@userid", supplier_area.UserID) }; var sql = "insert into T_SUPPLIERS_AREA(aid,createdatetime,state,userid)"; sql += "values(@aid,@createdatetime,@state,@userid)"; dbContext.Database.ExecuteSqlCommand(sql, paras); dbContext.SaveChanges(); } tran.Commit();//提交事务 return(1); } } }
public int Update(T_SUPPLIERS_AREA entity) { throw new NotImplementedException(); }