Advertisement
Shimmy

Untitled

Jul 3rd, 2013
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.99 KB | None | 0 0
  1.           var storeBusiness = Context.BusinessGraph.SingleOrDefault(b => b.BusinessId == business.BusinessId);
  2.           var entry = Context.Entry(storeBusiness);
  3.  
  4.           //properties
  5.           entry.CurrentValues.SetValues(business);
  6.  
  7.           //image
  8.           var imageProp = entry.Property(e => e.Image);
  9.           if (imageProp.CurrentValue == null)
  10.             imageProp.IsModified = false;
  11.  
  12.           //business type
  13.           if (storeBusiness.BusinessType.BusinessTypeId != business.BusinessType.BusinessTypeId)
  14.           {
  15.             var businessType = Context.FindOrAttach(business.BusinessType);
  16.             storeBusiness.BusinessType = businessType;
  17.           }
  18.  
  19.           //categories          
  20.           foreach (var category in storeBusiness.Categories.ToArray())
  21.           {
  22.             if (!business.Categories.Any(c => c.CategoryId == category.CategoryId))
  23.               storeBusiness.Categories.Remove(category);
  24.           }
  25.           foreach (var category in business.Categories)
  26.           {
  27.             var attached = Context.FindOrAttach(category);
  28.             storeBusiness.Categories.Add(attached);
  29.           }
  30.  
  31.           //branches
  32.           foreach (var branch in storeBusiness.Branches.ToArray())
  33.           {
  34.             if (!business.Branches.Any(b => b.BranchId == branch.BranchId))
  35.               storeBusiness.Branches.Remove(branch);
  36.           }
  37.           foreach (var branch in business.Branches)
  38.           {
  39.             if (branch.BranchId > 0)
  40.             {
  41.               var storeBranch = storeBusiness.Branches.Single(b => b.BranchId == branch.BranchId);
  42.               var branchEntry = Context.Entry(storeBranch);
  43.               branchEntry.CurrentValues.SetValues(branch);
  44.  
  45.               //branch type
  46.               if (storeBranch.BranchType.BranchTypeId != storeBranch.BranchType.BranchTypeId)
  47.               {
  48.                 var branchType = Context.FindOrAttach(branch.BranchType);
  49.                 storeBranch.BranchType = branchType;
  50.               }
  51.  
  52.               //phones
  53.               foreach (var phone in storeBranch.Phones.ToArray())
  54.               {
  55.                 if (!branch.Phones.Any(p => p.PhoneId == phone.PhoneId))
  56.                   storeBranch.Phones.Remove(phone);
  57.               }
  58.               foreach (var phone in branch.Phones)
  59.               {
  60.                 if (phone.PhoneId > 0)
  61.                 {
  62.                   var storePhone = storeBranch.Phones.Single(p => p.PhoneId == phone.PhoneId);
  63.                   var phoneEntry = Context.Entry(storePhone);
  64.                   phoneEntry.CurrentValues.SetValues(phone);
  65.                 }
  66.                 else
  67.                   storeBranch.Phones.Add(phone);
  68.               }
  69.  
  70.               //custom fields
  71.               foreach (var custField in storeBranch.CustomFields.ToArray())
  72.               {
  73.                 if (!branch.CustomFields.Any(cf => cf.CustomFieldId == custField.CustomFieldId))
  74.                   storeBranch.CustomFields.Remove(custField);
  75.               }
  76.               foreach (var custField in branch.CustomFields)
  77.               {
  78.                 if (custField.CustomFieldId > 0)
  79.                 {
  80.                   var storeCustField = storeBranch.CustomFields.Single(cf => cf.CustomFieldId == custField.CustomFieldId);
  81.                   var custFieldEntry = Context.Entry(storeCustField);
  82.                   custFieldEntry.CurrentValues.SetValues(custField);
  83.                 }
  84.                 else
  85.                   storeBranch.CustomFields.Add(custField);
  86.               }
  87.  
  88.               //opening times
  89.               storeBranch.OpeningTimes.Clear();
  90.               foreach (var ot in branch.OpeningTimes)
  91.               {
  92.                 foreach (var wp in ot.WorkingPeriods)
  93.                   wp.WorkingPeriodId = 0;
  94.                 ot.OpeningTimesId = 0;
  95.  
  96.                 storeBranch.OpeningTimes.Add(ot);
  97.               }
  98.             }
  99.             else
  100.             {
  101.  
  102.               storeBusiness.Branches.Add(branch);
  103.             }
  104.           }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement