/// <summary> /// Wraps a call to EntityFramework Core Add. The call is made with a mapped entity (DataAccess.Models.Customer) instead of the domain model passed as a parameter. The DataAccess.Model is used to communicate with EF Core. /// /// EF Core Add: /// Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database for an entity with the given primary key values and this entity, if found, is attached to the context and returned. If no entity is found, then null is returned. /// </summary> /// <param name="entity"></param> /// <returns>Domain.Models.Customer</returns> public void Add(Domain.Models.Customer entity) { var mappedAddress = Mapper.MapAddress(entity); _context.Set <Address>().Add(mappedAddress); _context.SaveChanges(); _context.Entry(mappedAddress).Reload(); var mappedEntity = Mapper.MapCustomer(entity); mappedEntity.Address = mappedAddress.AddressId; _context.Set <Customer>().Add(mappedEntity); }
/// <summary> /// This method tries to update an entity in the database through setting EntityFramework Core's Entry property to EntityState.Modified. If the update fails an exception is thrown. If the update succeeds then the policies parameter object passed in is saved to the database. /// </summary> /// <param name="policies"></param> /// <param name="id"></param> /// <returns></returns> public async Task <bool> ModifyStateAsync(Domain.Models.Policies policies, int id) { var mappedPolicies = Mapper.MapPolicies(policies); /*_context.Entry(policies).State = EntityState.Modified;*/ _context.Entry(mappedPolicies).State = EntityState.Modified; try { await SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PoliciesExists(id)) { return(false); // policies not found } else { throw; } } return(true); // it worked, so return true }
/// <summary> /// This method tries to update an entity in the database through setting EntityFramework Core's Entry property to EntityState.Modified. If the update fails an exception is thrown. If the update succeeds then the claim parameter object passed in is saved to the database. /// </summary> /// <param name="claim"></param> /// <param name="id"></param> /// <returns></returns> public async Task <bool> ModifyStateAsync(Domain.Models.Claims claim, int id) { var mappedClaim = Mapper.MapClaims(claim); /*_context.Entry(claim).State = EntityState.Modified;*/ _context.Entry(mappedClaim).State = EntityState.Modified; try { await SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ClaimExists(id)) { return(false); // claim not found } else { throw; } } return(true); // it worked, so return true }
/// <summary> /// This method tries to update an entity in the database through setting EntityFramework Core's Entry property to EntityState.Modified. If the update fails an exception is thrown. If the update succeeds then the invoice parameter object passed in is saved to the database. /// </summary> /// <param name="invoice"></param> /// <param name="id"></param> /// <returns></returns> public async Task <bool> ModifyStateAsync(Domain.Models.Invoice invoice, int id) { var mappedInvoice = Mapper.MapInvoice(invoice); /*_context.Entry(invoice).State = EntityState.Modified;*/ _context.Entry(mappedInvoice).State = EntityState.Modified; try { await SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!InvoiceExists(id)) { return(false); // invoice not found } else { throw; } } return(true); // it worked, so return true }