adding customers and addresses?

7 месяцев назад
I'm updating a plugin I wrote for 4.2 that imports orders and customers from an eBay store into nopCommerce.  

Scratching my head over this new way of tracking customer addresses.    Here is what I *THINK* I need to do....


            // If buyer's email exists in nopCommerce use that customer record, else add new guest customer...
            Customer cust = await _customerService.GetCustomerByEmailAsync(ordEBay.BuyerEmail) ?? await _customerService.InsertGuestCustomerAsync();

            // Create a nop address from eBay specified info...
            Address addrNop = new Address { CreatedOnUtc = DateTime.UtcNow };
            addrNop = await FillNopAddressFromEBay(ordEBay);

            // See if this is a new customer or updating a returning customer....
            if (cust.Email == null)
            {
                cust.Email = ordEBay.BuyerEmail;

                cust.Company = addrNop.Company;
                cust.FirstName = addrNop.FirstName;
                cust.LastName = addrNop.LastName;
                cust.Phone = addrNop.PhoneNumber;
                cust.StreetAddress = addrNop.Address1;
                cust.StreetAddress2 = addrNop.Address2;
                cust.City = addrNop.City;
                cust.StateProvinceId = addrNop.StateProvinceId ?? -1;
                cust.ZipPostalCode = addrNop.ZipPostalCode;
                cust.CountryId = addrNop.CountryId ?? -1;
                cust.County = addrNop.County;
                cust.Phone = addrNop.PhoneNumber;

                // Add this address to the mapping...
                await _customerService.InsertCustomerAddressAsync(cust, addrNop);
            }


The last line throws an exception:

The INSERT statement conflicted with the FOREIGN KEY constraint "Customer_Addresses_Target". The conflict occurred in database "DB_8424_nopcommerce", table "dbo.Address", column 'Id'. The statement has been terminated.
7 месяцев назад
After you create a new address, you need to insert it.

await _addressService.InsertAddressAsync(addrNop);