public string SaveCustomer(Customer customer) { try { using (var context = new BAPOCOs()) { context.ContextOptions.LazyLoadingEnabled = false; //Add the customer graph into the context context.Contacts.Attach(customer); context.ObjectStateManager.ChangeObjectState(customer, StateHelpers.GetEquivalentEntityState(customer.State)); foreach (Reservation reservation in customer.Reservations.ToList()) { context.ObjectStateManager.ChangeObjectState(reservation, StateHelpers.GetEquivalentEntityState(reservation.State)); } context.SaveChanges(); return("OK"); } } catch (Exception ex) { return(ex.Message); } }
public static void ApplyStateChanges(this DbContext context) { foreach (var entity in context.ChangeTracker.Entries <IObjectsWithState>()) { IObjectsWithState stateInfo = entity.Entity; entity.State = StateHelpers.ConvertState(stateInfo.State); } }
public static void ApplyStateChanges(this IContext context) { foreach (var entry in (context as DbContext).ChangeTracker.Entries <IAudit>()) { IAudit stateInfo = entry.Entity; entry.State = StateHelpers.ConvertState(stateInfo.State); } }
/// <summary> /// Updates the entiy asynchronous. /// </summary> /// <param name="tDTo">The t d to.</param> /// <returns></returns> public async Task <int> UpdateEntiyAsync(EmployeePayHistoryDto tDTo) { var dbentity = typeAdapter.ConvertDtoToEntities(tDTo); dbentity.State = State.Modified; context.ChangeObjectState <EmployeePayHistory>(dbentity, StateHelpers.GetEquivalentEntityState(dbentity.State)); return(await uow.SaveAsync()); }
/// <summary> /// Update the EmployeePayHistoryEntiy. /// </summary> /// <param name="Entiy">The Entiy.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</return public bool UpdateEntiy(EmployeePayHistoryDto t) { var dbentity = typeAdapter.ConvertDtoToEntities(t); dbentity.State = State.Modified; context.ChangeObjectState <EmployeePayHistory>(dbentity, StateHelpers.GetEquivalentEntityState(dbentity.State)); uow.Save(); return(true); }
public void InsertOrUpdate(Products products) { if (products.Id == default(int)) { context.Entry(products).State = EntityState.Added; } else { context.Products.Add(products); context.Entry(products).State = StateHelpers.ConvertState(products.State); } }
/// <summary> /// Updates the with attach entiy asynchronous. /// </summary> /// <param name="tDTo">The t d to.</param> /// <returns></returns> public async Task <int> UpdateWithAttachEntiyAsync(EmployeePayHistoryDto tDTo) { var dbentity = typeAdapter.ConvertDtoToEntities(tDTo); if (StateHelpers.GetEquivalentEntityState(dbentity.State) == StateHelpers.GetEquivalentEntityState(State.Detached)) { entiesrepository.Attach(dbentity); } dbentity.State = State.Modified; context.ChangeObjectState <EmployeePayHistory>(dbentity, StateHelpers.GetEquivalentEntityState(dbentity.State)); return(await uow.SaveAsync()); }
/// <summary> /// Update With Attach the EmployeePayHistoryEntiy. /// </summary> /// <param name="Entiy">The Entiy.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</return public bool UpdateWithAttachEntiy(EmployeePayHistoryDto t) { var dbentity = typeAdapter.ConvertDtoToEntities(t); if (StateHelpers.GetEquivalentEntityState(dbentity.State) == StateHelpers.GetEquivalentEntityState(State.Detached)) { entiesrepository.Attach(dbentity); } dbentity.State = State.Modified; context.ChangeObjectState <EmployeePayHistory>(dbentity, StateHelpers.GetEquivalentEntityState(dbentity.State)); uow.Save(); return(true); }
/// <summary> /// Updates the employee /// </summary> /// <param name="employee"> /// The employee. /// </param> /// <returns> /// The update employee 2. /// </returns> /// <remarks> /// For WCF,because wcf cannot persistent ef4 objectcontext /// we also need change entity state then save it. Reference book 'Programming Entity framework 4' /// </remarks> public bool UpdateEmployeeByAttachEntity(Employee employee) { IObjectContext context = RepositoryHelper.GetDbContext(); IUnitOfWork uow = RepositoryHelper.GetUnitOfWork(context); EmployeeRepository employRepository = RepositoryHelper.GetEmployeeRepository(context); employRepository.Attach(employee); employee.rowguid = System.Guid.NewGuid(); employee.State = State.Modified; //To avoid this error when update record // "The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated." // exec sp_executesql N'update [HumanResources].[Employee] employee.ModifiedDate = System.DateTime.Now; context.ChangeObjectState(employee, StateHelpers.GetEquivalentEntityState(employee.State)); uow.Save(); return(true); }
public MaifestRequest PrepareManifestRequest(ManifestInformation manifestInfo) { MaifestRequest request = new MaifestRequest(); if (manifestInfo != null) { dynamic manifest_Data = manifestInfo.ManifestData[0]; dynamic transporters = manifestInfo.Transporter; dynamic worklines = manifestInfo.WorkLine; dynamic worklineInstructionhandling = manifestInfo.WorkLineInstructionHandling; dynamic attachment = manifestInfo.Attachment; Address mailingAddress = null; Address siteAddress = null; //dynamic state = null; //ManagementMethod idNumber = null; try { if (manifest_Data != null) { request.ManifestTrackingNumber = manifest_Data.Name; request.Status = "ReadyForSignature"; request.SubmissionType = "DataImage5Copy"; request.Rejection = false; request.Residue = false; request.Import = false; request.ContainsPreviousRejectOrResidue = false; #region Generator object var generator = new DesignatedFacility(); generator.EpaSiteId = manifest_Data.Generator_Old__r?.EPA_ID__c; generator.Name = manifest_Data.Generator_Old__r?.Name; generator.Modified = true; generator.Registered = true; #region Mailing Address mailingAddress = new Address(); mailingAddress.Address1 = manifest_Data.Generator_Old__r?.Manifest_Address__c; mailingAddress.City = manifest_Data.Generator_Old__r?.ManifestCity__c; mailingAddress.Zip = manifest_Data.Generator_Old__r?.Manifest_Postal_Code__c; mailingAddress.State = new State(); mailingAddress.State.Code = manifest_Data.Generator_Old__r?.Manifest_State_Province_Code__c != null? StateHelpers.GetStateCode(manifest_Data.Generator_Old__r?.Manifest_State_Province_Code__c) : null; mailingAddress.State.Name = mailingAddress.State.Code; generator.MailingAddress = mailingAddress; #endregion #region siteAddress siteAddress = new Address(); siteAddress.Address1 = manifest_Data.Generator_Old__r?.ShippingAddress?.street.Length < 50 ? manifest_Data.Generator_Old__r?.ShippingAddress?.street : manifest_Data.Generator_Old__r?.ShippingAddress?.street.Substring(0, 49); siteAddress.City = manifest_Data.Generator_Old__r?.ShippingAddress?.city; siteAddress.Zip = manifest_Data.Generator_Old__r?.ShippingAddress?.postalCode; siteAddress.State = new State(); siteAddress.State.Code = manifest_Data.Generator_Old__r?.ShippingAddress?.state; siteAddress.State.Name = manifest_Data.Generator_Old__r?.ShippingAddress?.state; generator.SiteAddress = siteAddress; #endregion #region PaperSignature generator.PaperSignatureInfo = new PaperSignatureInfo(); generator.PaperSignatureInfo.PrintedName = manifest_Data.Generator_Signer__c; generator.PaperSignatureInfo.SignatureDate = manifest_Data.Pick_Up_Date__c; #endregion generator.Contact = new Contact(); generator.Contact.Phone = new Phone(); generator.Contact.Phone.Number = ""; // to do generator.EmergencyPhone = new Phone(); generator.EmergencyPhone.Number = "";// to do request.Generator = generator; #endregion #region Transporter object request.Transporters = null; if (transporters != null) { request.Transporters = new List <DesignatedFacility>(); int index = 0; foreach (var transporter in transporters) { var transport = new DesignatedFacility(); transport.EpaSiteId = transporter.EPA_ID__c; transport.Name = transporter.Transporter__r?.Name; transport.Registered = true; transport.Order = index.ToString(); #region Mailing Address mailingAddress = new Address(); mailingAddress.Address1 = transporter.Transporter__r?.Address__c; mailingAddress.City = transporter.Transporter__r?.City__c; mailingAddress.Zip = transporter.Transporter__r?.Postal_Code__c; mailingAddress.State = new State(); mailingAddress.State.Code = transporter.Transporter__r?.State_Code__c; mailingAddress.State.Name = transporter.Transporter__r?.State_Code__c; transport.MailingAddress = mailingAddress; #endregion #region SiteAddress siteAddress = new Address(); siteAddress.Address1 = transporter.Transporter__r?.Address__c; siteAddress.City = transporter.Transporter__r?.City__c; siteAddress.Zip = transporter.Transporter__r?.Postal_Code__c; siteAddress.State = new State(); siteAddress.State.Code = transporter.Transporter__r?.State_Code__c; siteAddress.State.Name = transporter.Transporter__r?.State_Code__c; transport.SiteAddress = mailingAddress; #endregion #region PaperSignature transport.PaperSignatureInfo = new PaperSignatureInfo(); transport.PaperSignatureInfo.SignatureDate = manifest_Data?.Pick_Up_Date__c; if (index == 0) { transport.PaperSignatureInfo.PrintedName = transporter.Manifest__r?.Transporter_1_Signer__c; } else if (index == 1) { transport.PaperSignatureInfo.PrintedName = transporter.Manifest__r?.Transporter_2_Signer__c; } else { transport.PaperSignatureInfo.PrintedName = transporter.Manifest__r?.Transporter_3_Signer__c; } #endregion transport.Contact = new Contact(); transport.Contact.Phone = new Phone(); transport.Contact.Phone.Number = ""; // to do request.Transporters.Add(transport); index = index + 1; } } #endregion #region designatedFacility Object var designatedFacility = new DesignatedFacility(); designatedFacility.EpaSiteId = manifest_Data.Manifest_Destination__r?.EPA_ID__c; designatedFacility.Name = manifest_Data.Manifest_Destination__r?.Facility_Name__c; designatedFacility.Registered = true; #region Mailing Address mailingAddress = new Address(); mailingAddress.Address1 = manifest_Data.Manifest_Destination__r?.Address__c; mailingAddress.City = manifest_Data.Manifest_Destination__r?.City__c; mailingAddress.Zip = manifest_Data.Manifest_Destination__r?.ZipCode__c; mailingAddress.State = new State(); mailingAddress.State.Code = manifest_Data.Manifest_Destination__r?.State__c; mailingAddress.State.Name = manifest_Data.Manifest_Destination__r?.State__c; designatedFacility.MailingAddress = mailingAddress; #endregion #region siteAddress siteAddress = new Address(); siteAddress.Address1 = manifest_Data.Manifest_Destination__r?.Address__c; siteAddress.City = manifest_Data.Manifest_Destination__r?.City__c; siteAddress.Zip = manifest_Data.Manifest_Destination__r?.ZipCode__c; siteAddress.State = new State(); siteAddress.State.Code = manifest_Data.Manifest_Destination__r?.State__c; siteAddress.State.Name = manifest_Data.Manifest_Destination__r?.State__c; designatedFacility.SiteAddress = siteAddress; #endregion #region PaperSignature designatedFacility.PaperSignatureInfo = new PaperSignatureInfo(); designatedFacility.PaperSignatureInfo.PrintedName = manifest_Data.TSDF_Signer__c; designatedFacility.PaperSignatureInfo.SignatureDate = manifest_Data.TSDF_Received_Date__c != null? DateTime.Parse(manifest_Data.TSDF_Received_Date__c + "T00:00:00.000Z") : manifest_Data.TSDF_Received_Date__c; #endregion designatedFacility.Contact = new Contact(); designatedFacility.Contact.Phone = new Phone(); designatedFacility.Contact.Phone.Number = ""; // to do request.DesignatedFacility = designatedFacility; #endregion #region Waste object request.Wastes = new List <Waste>(); foreach (var _workline in worklines) { var waste = new Waste(); waste.DotHazardous = _workline.SVMXC__Service_Order_Line__c?.DOTID__c != null ? true : false; waste.LineNumber = _workline.SVMXC__Service_Order_Line__c?.Manifest_Line__c; waste.EpaWaste = _workline.SVMXC__Service_Order_Line__c?.Profile__r?.US_EPA_Haz_Waste__c == "Yes" ? true : false; waste.Br = false; waste.Pcb = false; if (waste.DotHazardous == true) { waste.DotInformation = new DotInformation(); waste.DotInformation.IdNumber = new ManagementMethod(); waste.DotInformation.IdNumber.Code = _workline.SVMXC__Service_Order_Line__c?.DOTID__c; waste.DotInformation.PrintedDotInformation = _workline.SVMXC__Service_Order_Line__c?.U_S_DOT_Description_9b__c != null ? _workline.SVMXC__Service_Order_Line__c?.U_S_DOT_Description_9b__c : _workline.SVMXC__Service_Order_Line__c?.US_DOT_Description_Override__c; } if (waste.DotHazardous == false) { waste.WasteDescription = _workline.SVMXC__Service_Order_Line__c?.U_S_DOT_Description_9b__c != null ? _workline.SVMXC__Service_Order_Line__c?.U_S_DOT_Description_9b__c : _workline.SVMXC__Service_Order_Line__c?.US_DOT_Description_Override__c; } waste.Quantity = new Quantity(); waste.Quantity.ContainerNumber = _workline.SVMXC__Service_Order_Line__c?.Container_Qty__c; waste.Quantity.ContainerType = new ManagementMethod(); waste.Quantity.ContainerType.Code = _workline.SVMXC__Service_Order_Line__c?.Container_UOM__c; waste.Quantity.QuantityQuantity = _workline.SVMXC__Service_Order_Line__c?.Total_Quantity_11_Manifest__c; waste.Quantity.UnitOfMeasurement = new ManagementMethod(); waste.Quantity.UnitOfMeasurement.Code = _workline.SVMXC__Service_Order_Line__c?.Unit_of_Measure_12__c; waste.ManagementMethod = new ManagementMethod(); waste.ManagementMethod.Code = _workline.SVMXC__Service_Order_Line__c?.Management_Method_Code__c != null ? _workline.SVMXC__Service_Order_Line__c?.Management_Method_Code__c : _workline.SVMXC__Service_Order_Line__c?.Management_Method_Code_Override__c; waste.DiscrepancyResidueInfo = new DiscrepancyResidueInfo(); waste.DiscrepancyResidueInfo.WasteQuantity = false; waste.DiscrepancyResidueInfo.WasteType = false; waste.DiscrepancyResidueInfo.Residue = false; waste.DiscrepancyResidueInfo.DiscrepancyComments = "no discrepancy"; request.Wastes.Add(waste); } #endregion #region AdditionalInfo & PrintedDocument request.AdditionalInfo = new AdditionalInfo(); request.AdditionalInfo.Comments = null; request.AdditionalInfo.HandlingInstructions = worklineInstructionhandling != null ? worklineInstructionhandling : null; request.PrintedDocument = new PrintedDocument(); request.PrintedDocument.Name = attachment.Name; request.PrintedDocument.Size = attachment.BodyLength; request.PrintedDocument.MimeType = "APPLICATION_PDF"; #endregion } } catch (Exception ex) { } } return(request); }
public void ChangeState <T>(ObjectState state, T entity) where T : class { ObjectStateManager.ChangeObjectState(entity, StateHelpers.GetEquivalentEntityState(state)); }