private bool ConstructValidateAndUpdate(IEnumerable<ShipToAddressImport> entities)
        {
            using (var context = ObjectFactory.Container.GetNestedContainer())
            {
                foreach (var entity in entities)
                {
                    var outlet = context.GetInstance<CokeDataContext>().tblCostCentre.FirstOrDefault(p => p
                         .Cost_Centre_Code.Trim().ToLower() ==
                                entity.OutletCode.Trim().ToLower()
                                && p.CostCentreType == (int)CostCentreType.Outlet);
                   
                   
                   if (outlet == null)
                    {
                        var result = new List<ValidationResult>
                                         {
                                             new ValidationResult(
                                                 string.Format("outlet code=>{0} not found",
                                                               entity.OutletCode))
                                         };

                        validations.Add(new ImportValidationResultInfo()
                                            {
                                                Description = entity.OutletName,
                                                Entity = new ShipToAddress(Guid.NewGuid()),
                                                Results = result
                                            });
                    }
                    else
                    {

                        var address = context.GetInstance<CokeDataContext>().tblShipToAddress.FirstOrDefault(
                            n =>
                            n.CostCentreId == outlet.Id && n.Code != null &&
                            n.Code.ToLower() == entity.ShipToCode.ToLower() && n.Name != null &&
                            (n.Name.ToLower() == entity.ShipToName.ToLower()) &&
                            (n.PostalAddress != null &&
                             (n.PostalAddress.ToLower() == entity.PostalAddress.ToLower())));
                        var dt = DateTime.Now;
                        bool isNew = false;
                        if (address == null)
                        {
                            address = new tblShipToAddress
                                          {
                                              IM_DateCreated = dt,
                                              IM_Status = (int) EntityStatus.Active
                                              ,
                                              Id = Guid.NewGuid()
                                          };
                            context.GetInstance<CokeDataContext>().tblShipToAddress.AddObject(address);
                            isNew = true;
                        }

                        if (isNew || HasChanged(entity, address, outlet.Id))
                        {
                            address.CostCentreId = outlet.Id;
                            address.Name = entity.ShipToName ?? outlet.Name;
                            address.PhysicalAddress = entity.PostalAddress;
                            address.PostalAddress = entity.PostalAddress;
                            address.Description = entity.ShipToCode ?? string.Empty;
                            address.Code = entity.ShipToCode ?? string.Empty;
                            address.Latitude = 0;
                            address.Longitude = 0;
                            address.IM_DateLastUpdated = dt;
                            try
                            {
                                context.GetInstance<CokeDataContext>().SaveChanges();
                            }
                            catch (DbEntityValidationException ex)
                            {
                                foreach (var error in ex.EntityValidationErrors)
                                {
                                    Console.WriteLine("====================");

                                    FileUtility.LogError(string.Format("Entity {0} in state {1} has validation errors:",
                                                                       error.Entry.Entity.GetType().Name,
                                                                       error.Entry.State));
                                    foreach (var ve in error.ValidationErrors)
                                    {
                                        Console.WriteLine("\tProperty: {0}, Error: {1}",
                                                          ve.PropertyName, ve.ErrorMessage);
                                    }
                                    Console.WriteLine();
                                }
                                throw;
                            }

                        }
                    }
                }
            }
            return true;

        }
        private bool HasChanged(ShipToAddressImport import, tblShipToAddress existing,Guid outletId)
        {
            var changed = existing.CostCentreId == outletId &&
                          ((existing.Code != null &&
                            existing.Code.Trim().ToLower() != import.ShipToCode.Trim().ToLower()));

            return changed;
        }
 /// <summary>
 /// Create a new tblShipToAddress object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="costCentreId">Initial value of the CostCentreId property.</param>
 /// <param name="iM_DateCreated">Initial value of the IM_DateCreated property.</param>
 /// <param name="iM_DateLastUpdated">Initial value of the IM_DateLastUpdated property.</param>
 /// <param name="iM_Status">Initial value of the IM_Status property.</param>
 public static tblShipToAddress CreatetblShipToAddress(global::System.Guid id, global::System.Guid costCentreId, global::System.DateTime iM_DateCreated, global::System.DateTime iM_DateLastUpdated, global::System.Int32 iM_Status)
 {
     tblShipToAddress tblShipToAddress = new tblShipToAddress();
     tblShipToAddress.Id = id;
     tblShipToAddress.CostCentreId = costCentreId;
     tblShipToAddress.IM_DateCreated = iM_DateCreated;
     tblShipToAddress.IM_DateLastUpdated = iM_DateLastUpdated;
     tblShipToAddress.IM_Status = iM_Status;
     return tblShipToAddress;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the tblShipToAddress EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddTotblShipToAddress(tblShipToAddress tblShipToAddress)
 {
     base.AddObject("tblShipToAddress", tblShipToAddress);
 }
       private bool CanInsert(tblShipToAddress dto)
       {
           using (var ctx = new CokeDataContext(Con))
           {
               return
                   ctx.tblShipToAddress.Any(
                       p => (
                                p.CostCentreId == dto.CostCentreId &&
                                (p.Code != null && p.Code.ToLower() == dto.Code.ToLower())
                                || (p.Name != null && p.Name.ToLower() == dto.Name)) &&
                            ((p.PhysicalAddress != null &&
                              p.PhysicalAddress.Equals(dto.PhysicalAddress, StringComparison.InvariantCultureIgnoreCase)) ||
                             (p.PostalAddress != null &&
                              p.PostalAddress.Equals(dto.PostalAddress, StringComparison.CurrentCultureIgnoreCase))));

           }
       }
 private void Insert(tblShipToAddress dto)
 {
     using (var ctx = new CokeDataContext(Con))
     {
         ctx.tblShipToAddress.AddObject(dto);
         ctx.SaveChanges();
     }
 }
        private async Task<IEnumerable<tblShipToAddress>> ConstructEntities(IEnumerable<ShipToAddressImport> entities)
        {
            return await Task.Run(() =>
                                      {
                                          var newOutletAddresses = new List<tblShipToAddress>();
                                         

                                          foreach (var entity in entities)
                                          {
                                              var outlet =
                                                  _costCentreRepository.GetByCode(entity.OutletCode,
                                                                                  CostCentreType.Outlet, true) as
                                                  Outlet;
                                              if (outlet == null)
                                              {
                                                  var result=new List<ValidationResult>
                                                                 {
                                                                     new ValidationResult(
                                                                         string.Format("outlet code=>{0} not found",
                                                                                       entity.OutletCode))
                                                                 };

                                                  validations.Add(new ImportValidationResultInfo()
                                                                      {
                                                                          Description = entity.OutletName,
                                                                          Entity = new ShipToAddress(Guid.NewGuid()),
                                                                          Results = result
                                                                      });
                                              }
                                              else
                                              {

                                                  var address =
                                                      _ctx.tblShipToAddress.FirstOrDefault(
                                                          n =>
                                                          n.CostCentreId == outlet.Id &&n.Code !=null && n.Code.ToLower()==entity.ShipToCode.ToLower() && n.Name != null &&
                                                          (n.Name.ToLower() == entity.ShipToName.ToLower()) &&
                                                          (n.PostalAddress != null &&
                                                           (n.PostalAddress.ToLower() == entity.PostalAddress.ToLower())));
                                                  var dt = DateTime.Now;
                                                  if (address == null)
                                                  {
                                                      address = new tblShipToAddress
                                                                    {
                                                                        IM_DateCreated = dt,
                                                                        IM_Status = (int) EntityStatus.Active
                                                                        ,
                                                                        Id = Guid.NewGuid()
                                                                    };
                                                      _ctx.tblShipToAddress.AddObject(address);
                                                  }
                                                  address.CostCentreId = outlet.Id;
                                                  address.Name = entity.ShipToName ?? outlet.Name;
                                                  address.PhysicalAddress = entity.PostalAddress;
                                                  address.PostalAddress = entity.PostalAddress;
                                                  address.Description = entity.ShipToCode ?? string.Empty;
                                                  address.Code = entity.ShipToCode ?? string.Empty;
                                                  address.Latitude = 0;
                                                  address.Longitude = 0;
                                                  address.IM_DateLastUpdated = dt;
                                                  
                                                  newOutletAddresses.Add(address);
                                              }
                                          }
                                          return newOutletAddresses;
                                      });
        }