public void SaveProduct(ProductModels product) { using (var context = new AnimaliaPetShopEntities()) { using (var dbContextTransaction = context.Database.BeginTransaction()) { try { var productDb = MapperObject.CreateProductDb(product); //AddNewProduct AddProduct(productDb); context.SaveChanges(); dbContextTransaction.Commit(); } catch (Exception exepction) { dbContextTransaction.Rollback(); throw exepction; } } } }
public void GuardarCliente(ClienteModel clienteModel) { using (var context = new AnimaliaPetShopEntities()) { using (var dbContextTransaction = context.Database.BeginTransaction()) { try { db = Factory.Factory.CreateContextDataBase(); clienteModel.FechaCompra = DateTime.Parse(clienteModel.FechaCompra1).Date; clienteModel.FechaProximaCompra = DateTime.Parse(clienteModel.FechaCompra2).Date; var clienteDb = MapperObject.CreateClienteDb(clienteModel); if (clienteDb.IdCliente == null || clienteDb.IdCliente == Guid.Empty) { clienteDb.IdCliente = Guid.NewGuid(); db.Cliente.Add(clienteDb); SaveProductsId(clienteModel, clienteDb.IdCliente); db.SaveChanges(); dbContextTransaction.Commit(); } else { SaveProductsId(clienteModel, clienteDb.IdCliente); db.Cliente.Attach(clienteDb); db.Entry(clienteDb).State = EntityState.Modified; db.SaveChanges(); dbContextTransaction.Commit(); } } catch (Exception e) { throw new Exception(e.Message.ToString()); } } } }
public void SaveProvider(ProviderModel providerModel) { using (var context = new AnimaliaPetShopEntities()) { using (var dbContextTransaction = context.Database.BeginTransaction()) { try { db = Factory.Factory.CreateContextDataBase(); providerModel.Fecha = DateTime.Now; var providerDb = MapperObject.CreateProviderDb(providerModel); if (providerDb.IdProveedor == null || providerDb.IdProveedor == Guid.Empty) { providerDb.IdProveedor = Guid.NewGuid(); db.Proveedor.Add(providerDb); SaveProductsId(providerModel, providerDb.IdProveedor); db.SaveChanges(); dbContextTransaction.Commit(); } else { SaveProductsId(providerModel, providerDb.IdProveedor); db.Proveedor.Attach(providerDb); db.Entry(providerDb).State = EntityState.Modified; db.SaveChanges(); dbContextTransaction.Commit(); } } catch (Exception e) { throw new Exception(e.Message.ToString()); } } } }