private CRMUserRole GetSaveEntity() { var entity = new CRMUserRole(); if (string.IsNullOrEmpty(txtID.Text.Trim()) == false) { entity.ID = int.Parse(txtID.Text.Trim()); } if (string.IsNullOrEmpty(txtUserID.Text.Trim()) == false) { entity.UserID = int.Parse(txtUserID.Text.Trim()); } if (string.IsNullOrEmpty(txtRoleID.Text.Trim()) == false) { entity.RoleID = int.Parse(txtRoleID.Text.Trim()); } return(entity); }
//---------------保存CRMUserRole--------------------------- public CRMUserRole Save(CRMUserRole entity) { if (this.dataCtx.Connection != null) { if (this.dataCtx.Connection.State == ConnectionState.Closed) { this.dataCtx.Connection.Open(); } } DbTransaction tran = this.dataCtx.Connection.BeginTransaction(); dataCtx.Transaction = tran; try { var qry = from t in CRMUserRoles where t.ID == entity.ID select t; var obj = qry.SingleOrDefault(); if (obj != null) { this.CopyEntity(obj, entity); } else { this.CRMUserRoles.InsertOnSubmit(entity); } this.dataCtx.SubmitChanges(); tran.Commit(); return(entity); } catch (Exception ex) { tran.Rollback(); throw ex; } finally { dataCtx.Connection.Close(); } }
public CRMUser Save(CRMUser entity, IList <CRMRole> RoleList) { if (this.dataCtx.Connection != null) { if (this.dataCtx.Connection.State == ConnectionState.Closed) { this.dataCtx.Connection.Open(); } } DbTransaction tran = this.dataCtx.Connection.BeginTransaction(); dataCtx.Transaction = tran; try { var qry = from t in CRMUsers where t.UserID == entity.UserID select t; var obj = qry.SingleOrDefault(); //encrypt password,如果没输入password,不更改数据库password if (string.IsNullOrEmpty(entity.Password)) { if (obj != null) { entity.Password = obj.Password; } } else { entity.Password = this.Encypt(entity.UserName, entity.Password); } if (obj != null) { this.CopyEntity(obj, entity); } else { this.CRMUsers.InsertOnSubmit(entity); } this.dataCtx.SubmitChanges(); //delete Role relationship with User var qryDel = from t in CRMUserRoles where t.UserID == entity.UserID select t; foreach (var item in qryDel.ToList()) { this.CRMUserRoles.DeleteOnSubmit(item); } //add new foreach (var role in RoleList) { var p = new CRMUserRole(); p.UserID = entity.UserID; p.RoleID = role.RoleID; this.CRMUserRoles.InsertOnSubmit(p); } this.dataCtx.SubmitChanges(); tran.Commit(); return(entity); } catch (Exception ex) { tran.Rollback(); throw ex; } finally { dataCtx.Connection.Close(); } }
private CRMUserRole GetSaveEntity() { var entity = new CRMUserRole(); if (string.IsNullOrEmpty(txtID.Text.Trim()) == false) entity.ID = int.Parse(txtID.Text.Trim()); if (string.IsNullOrEmpty(txtUserID.Text.Trim()) == false) entity.UserID = int.Parse(txtUserID.Text.Trim()); if (string.IsNullOrEmpty(txtRoleID.Text.Trim()) == false) entity.RoleID = int.Parse(txtRoleID.Text.Trim()); return entity; }
//---------------保存CRMUserRole--------------------------- public CRMUserRole Save(CRMUserRole entity) { if (this.dataCtx.Connection != null) if (this.dataCtx.Connection.State == ConnectionState.Closed) this.dataCtx.Connection.Open(); DbTransaction tran = this.dataCtx.Connection.BeginTransaction(); dataCtx.Transaction = tran; try { var qry = from t in CRMUserRoles where t.ID == entity.ID select t; var obj = qry.SingleOrDefault(); if (obj != null) this.CopyEntity(obj, entity); else this.CRMUserRoles.InsertOnSubmit(entity); this.dataCtx.SubmitChanges(); tran.Commit(); return entity; } catch (Exception ex) { tran.Rollback(); throw ex; } finally { dataCtx.Connection.Close(); } }
public CRMUser Save(CRMUser entity, IList<CRMRole> RoleList) { if (this.dataCtx.Connection != null) if (this.dataCtx.Connection.State == ConnectionState.Closed) this.dataCtx.Connection.Open(); DbTransaction tran = this.dataCtx.Connection.BeginTransaction(); dataCtx.Transaction = tran; try { var qry = from t in CRMUsers where t.UserID == entity.UserID select t; var obj = qry.SingleOrDefault(); //encrypt password,���û����password�����������ݿ�password if (string.IsNullOrEmpty(entity.Password)) { if (obj != null) { entity.Password = obj.Password; } } else { entity.Password = this.Encypt(entity.UserName, entity.Password); } if (obj != null) this.CopyEntity(obj, entity); else this.CRMUsers.InsertOnSubmit(entity); this.dataCtx.SubmitChanges(); //delete Role relationship with User var qryDel = from t in CRMUserRoles where t.UserID == entity.UserID select t; foreach (var item in qryDel.ToList()) { this.CRMUserRoles.DeleteOnSubmit(item); } //add new foreach (var role in RoleList) { var p = new CRMUserRole(); p.UserID = entity.UserID; p.RoleID = role.RoleID; this.CRMUserRoles.InsertOnSubmit(p); } this.dataCtx.SubmitChanges(); tran.Commit(); return entity; } catch (Exception ex) { tran.Rollback(); throw ex; } finally { dataCtx.Connection.Close(); } }