public EstabelecimentoVM(usuario usuario) : base(usuario) { this.Estabelecimento = this.GetEstabelecimentoByID(this.ID); if (this.Estabelecimento != null) { this.As = this.Estabelecimento.As; this.Ate = this.Estabelecimento.Ate; this.Das = this.Estabelecimento.Das; this.De = this.Estabelecimento.De; this.Descricao = this.Estabelecimento.Descricao; this.CNPJ = this.Estabelecimento.CNPJ; this.Razao = this.Estabelecimento.Razao; this.IDAmbientacao = this.Estabelecimento.IDAmbientacao; } else { this.As = 0; this.Ate = 1; this.Das = 0; this.De = 1; this.Descricao = string.Empty; this.CNPJ = string.Empty; this.Razao = string.Empty; this.IDAmbientacao = 1; } }
public bool SaveChanges() { try { using (var db = new nosso_showEntities(Conexao.GetString())) { var u = db.usuario.Single(x => x.ID == this.Usuario.ID); var e = db.usuario_estabelecimento.SingleOrDefault(x => x.IDUsuario == this.Usuario.ID); u.Email = this.Email; u.Nome = this.Nome; u.Username = this.Username; if (e == null) { e = new usuario_estabelecimento(); } e.CNPJ = Regex.Replace(this.CNPJ, @"[^0-9]", string.Empty); e.De = this.De; e.Ate = this.Ate; e.Das = this.Das; e.As = this.As; e.Descricao = this.Descricao; e.IDAmbientacao = this.IDAmbientacao; e.IDUsuario = this.Usuario.ID; e.Razao = this.Razao; e.TipoUsuario = this.Usuario.Tipo; if (db.usuario_estabelecimento.Any(x => x.IDUsuario == u.ID)) { db.ObjectStateManager.ChangeObjectState(e, System.Data.EntityState.Modified); } else { db.usuario_estabelecimento.AddObject(e); } db.SaveChanges(); db.ObjectStateManager.ChangeObjectState(u, System.Data.EntityState.Modified); db.SaveChanges(); return(true); } } catch { return(false); } }