private static void EnsureSectionContext(SALE_CODE section) { using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var entity = db.Set<StoreEntity>().Where(b => b.ExStoreId == section.SHOP_SID).FirstOrDefault(); if (null == entity) { StoreSyncJob.SyncOne(section.SHOP_INFO); } } }
public static void SyncOne(SALE_CODE section) { EnsureSectionContext(section); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var sectionId = section.SID; var existBrand = db.Set<SectionEntity>().Where(b => b.ChannelSectionId == sectionId).FirstOrDefault(); var storeEntity = db.Set<StoreEntity>().Where(s => s.ExStoreId == section.SHOP_SID).FirstOrDefault(); if (existBrand == null) { db.Sections.Add(new SectionEntity() { ChannelSectionId = (int)section.SID, CreateDate = DateTime.Now, CreateUser = 0, Location = section.ADDRESS ?? string.Empty, Name = section.SALE_CODE_NAME ?? string.Empty, ContactPhone = string.Empty, StoreId = storeEntity.Id, Status = (int)DataStatus.Normal, UpdateDate = DateTime.Now, UpdateUser = 0 }); } else { existBrand.Name = section.SALE_CODE_NAME ?? string.Empty; existBrand.UpdateDate = section.OPT_UPDATE_TIME ?? DateTime.Now; existBrand.Location = section.ADDRESS ?? string.Empty; existBrand.StoreId = storeEntity.Id; db.Entry(existBrand).State = System.Data.EntityState.Modified; } db.SaveChanges(); } }