Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var storeBusiness = Context.BusinessGraph.SingleOrDefault(b => b.BusinessId == business.BusinessId);
- var entry = Context.Entry(storeBusiness);
- //properties
- entry.CurrentValues.SetValues(business);
- //image
- var imageProp = entry.Property(e => e.Image);
- if (imageProp.CurrentValue == null)
- imageProp.IsModified = false;
- //business type
- if (storeBusiness.BusinessType.BusinessTypeId != business.BusinessType.BusinessTypeId)
- {
- var businessType = Context.FindOrAttach(business.BusinessType);
- storeBusiness.BusinessType = businessType;
- }
- //categories
- foreach (var category in storeBusiness.Categories.ToArray())
- {
- if (!business.Categories.Any(c => c.CategoryId == category.CategoryId))
- storeBusiness.Categories.Remove(category);
- }
- foreach (var category in business.Categories)
- {
- var attached = Context.FindOrAttach(category);
- storeBusiness.Categories.Add(attached);
- }
- //branches
- foreach (var branch in storeBusiness.Branches.ToArray())
- {
- if (!business.Branches.Any(b => b.BranchId == branch.BranchId))
- storeBusiness.Branches.Remove(branch);
- }
- foreach (var branch in business.Branches)
- {
- if (branch.BranchId > 0)
- {
- var storeBranch = storeBusiness.Branches.Single(b => b.BranchId == branch.BranchId);
- var branchEntry = Context.Entry(storeBranch);
- branchEntry.CurrentValues.SetValues(branch);
- //branch type
- if (storeBranch.BranchType.BranchTypeId != storeBranch.BranchType.BranchTypeId)
- {
- var branchType = Context.FindOrAttach(branch.BranchType);
- storeBranch.BranchType = branchType;
- }
- //phones
- foreach (var phone in storeBranch.Phones.ToArray())
- {
- if (!branch.Phones.Any(p => p.PhoneId == phone.PhoneId))
- storeBranch.Phones.Remove(phone);
- }
- foreach (var phone in branch.Phones)
- {
- if (phone.PhoneId > 0)
- {
- var storePhone = storeBranch.Phones.Single(p => p.PhoneId == phone.PhoneId);
- var phoneEntry = Context.Entry(storePhone);
- phoneEntry.CurrentValues.SetValues(phone);
- }
- else
- storeBranch.Phones.Add(phone);
- }
- //custom fields
- foreach (var custField in storeBranch.CustomFields.ToArray())
- {
- if (!branch.CustomFields.Any(cf => cf.CustomFieldId == custField.CustomFieldId))
- storeBranch.CustomFields.Remove(custField);
- }
- foreach (var custField in branch.CustomFields)
- {
- if (custField.CustomFieldId > 0)
- {
- var storeCustField = storeBranch.CustomFields.Single(cf => cf.CustomFieldId == custField.CustomFieldId);
- var custFieldEntry = Context.Entry(storeCustField);
- custFieldEntry.CurrentValues.SetValues(custField);
- }
- else
- storeBranch.CustomFields.Add(custField);
- }
- //opening times
- storeBranch.OpeningTimes.Clear();
- foreach (var ot in branch.OpeningTimes)
- {
- foreach (var wp in ot.WorkingPeriods)
- wp.WorkingPeriodId = 0;
- ot.OpeningTimesId = 0;
- storeBranch.OpeningTimes.Add(ot);
- }
- }
- else
- {
- storeBusiness.Branches.Add(branch);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement