@@ -147,8 +147,15 @@ protected virtual int ProcessAddresses(
147147 {
148148 foreach ( var row in batch )
149149 {
150- ImportAddress ( "BillingAddress." , row , context , allCountries , allStateProvinces ) ;
151- ImportAddress ( "ShippingAddress." , row , context , allCountries , allStateProvinces ) ;
150+ if ( row . HasDataValue ( "BillingAddress.LastName" ) )
151+ {
152+ ImportAddress ( "BillingAddress." , row , context , allCountries , allStateProvinces ) ;
153+ }
154+
155+ if ( row . HasDataValue ( "ShippingAddress.LastName" ) )
156+ {
157+ ImportAddress ( "ShippingAddress." , row , context , allCountries , allStateProvinces ) ;
158+ }
152159 }
153160
154161 return _services . DbContext . SaveChanges ( ) ;
@@ -161,7 +168,7 @@ private void ImportAddress(
161168 Dictionary < string , int > allCountries ,
162169 Dictionary < Tuple < int , string > , int > allStateProvinces )
163170 {
164- // last name is mandatory for an address to be imported
171+ // last name is mandatory for an address to be imported or updated
165172 var lastName = row . GetDataValue < string > ( fieldPrefix + "LastName" ) ;
166173 if ( lastName . IsEmpty ( ) )
167174 return ;
@@ -207,7 +214,7 @@ private void ImportAddress(
207214 row . Entity . ShippingAddress = appliedAddress ;
208215 }
209216
210- _customerService . UpdateCustomer ( row . Entity ) ;
217+ _customerRepository . Update ( row . Entity ) ;
211218 }
212219
213220 protected virtual int ProcessGenericAttributes (
@@ -374,6 +381,8 @@ protected virtual int ProcessCustomers(
374381 var registeredRole = _customerService . GetCustomerRoleBySystemName ( SystemCustomerRoleNames . Registered ) ;
375382 var forumModeratorRole = _customerService . GetCustomerRoleBySystemName ( SystemCustomerRoleNames . ForumModerators ) ;
376383
384+ var customerQuery = _customerRepository . Table . Expand ( x => x . Addresses ) ;
385+
377386 foreach ( var row in batch )
378387 {
379388 Customer customer = null ;
@@ -385,18 +394,31 @@ protected virtual int ProcessCustomers(
385394 switch ( keyName )
386395 {
387396 case "Id" :
388- customer = _customerService . GetCustomerById ( id ) ;
397+ if ( id != 0 )
398+ {
399+ customer = customerQuery . FirstOrDefault ( x => x . Id == id ) ;
400+ }
389401 break ;
390402 case "CustomerGuid" :
391- var guid = row . GetDataValue < string > ( "CustomerGuid" ) ;
392- if ( guid . HasValue ( ) )
393- customer = _customerService . GetCustomerByGuid ( new Guid ( guid ) ) ;
403+ var customerGuid = row . GetDataValue < string > ( "CustomerGuid" ) ;
404+ if ( customerGuid . HasValue ( ) )
405+ {
406+ var guid = new Guid ( customerGuid ) ;
407+ customer = customerQuery . FirstOrDefault ( x => x . CustomerGuid == guid ) ;
408+ }
394409 break ;
395410 case "Email" :
396- customer = _customerService . GetCustomerByEmail ( email ) ;
411+ if ( email . HasValue ( ) )
412+ {
413+ customer = customerQuery . FirstOrDefault ( x => x . Email == email ) ;
414+ }
397415 break ;
398416 case "Username" :
399- customer = _customerService . GetCustomerByUsername ( row . GetDataValue < string > ( "Username" ) ) ;
417+ var userName = row . GetDataValue < string > ( "Username" ) ;
418+ if ( userName . HasValue ( ) )
419+ {
420+ customer = customerQuery . FirstOrDefault ( x => x . Username == userName ) ;
421+ }
400422 break ;
401423 }
402424
@@ -603,16 +625,19 @@ protected override void Import(IImportExecuteContext context)
603625 // ===========================================================================
604626 // Process addresses
605627 // ===========================================================================
606- if ( segmenter . HasColumn ( "BillingAddress.LastName" ) || segmenter . HasColumn ( "ShippingAddress.LastName" ) )
628+ try
607629 {
608- try
609- {
610- ProcessAddresses ( context , batch , allCountries , allStateProvinces ) ;
611- }
612- catch ( Exception exception )
613- {
614- context . Result . AddError ( exception , segmenter . CurrentSegment , "ProcessAddresses" ) ;
615- }
630+ _services . DbContext . AutoDetectChangesEnabled = true ;
631+
632+ ProcessAddresses ( context , batch , allCountries , allStateProvinces ) ;
633+ }
634+ catch ( Exception exception )
635+ {
636+ context . Result . AddError ( exception , segmenter . CurrentSegment , "ProcessAddresses" ) ;
637+ }
638+ finally
639+ {
640+ _services . DbContext . AutoDetectChangesEnabled = false ;
616641 }
617642 }
618643 }
0 commit comments