Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7406789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:36:39+00:00 2026-05-29T05:36:39+00:00

I have a Customers table with a foreign reference to the Addresses table. It

  • 0

I have a Customers table with a foreign reference to the Addresses table. It appears that AutoMapper is doing something to make EF think that my Address reference is a new record rather than updating the existing record.

This code updates the Address record correctly. It does not add a new one:

using (CSIntUnitOfWork uow = new CSIntUnitOfWork())
{
    CustomerRepository customerRepository = new CustomerRepository(uow, _resellerID);

    DataModels.Customer updateCustomer = customerRepository.GetByID(customer.CustomerID);

    updateCustomer.ResellerID = customer.ResellerID;
    updateCustomer.CustomerType = customer.CustomerType;
    updateCustomer.Password = customer.Password;
    updateCustomer.Comments = customer.Comments;

    updateCustomer.Address.ResellerID = customer.Address.ResellerID;
    updateCustomer.Address.AddressCode = customer.Address.AddressCode;
    updateCustomer.Address.AddressType = customer.Address.AddressType;
    updateCustomer.Address.CompanyName = customer.Address.CompanyName;
    updateCustomer.Address.LastName = customer.Address.LastName;
    updateCustomer.Address.FirstName = customer.Address.FirstName;

    uow.SaveChanges();
}

This code will always add a new Address record:

using (CSIntUnitOfWork uow = new CSIntUnitOfWork())
{
    CustomerRepository customerRepository = new CustomerRepository(uow, _resellerID);

    DataModels.Customer updateCustomer = customerRepository.GetByID(customer.CustomerID);

    Mapper.CreateMap<Customer, Customer>()
        .ForMember(dest => dest.CustomerID, opt => opt.Ignore());
    Mapper.Map(customer, updateCustomer);

    Mapper.CreateMap<Address, Address>()
        .ForMember(dest => dest.ID, opt => opt.Ignore());
    Mapper.Map(customer.Address, updateCustomer.Address);

    uow.SaveChanges();
}

Any ideas why this is happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:36:40+00:00Added an answer on May 29, 2026 at 5:36 am

    I just figured it out.

    AutoMapper is mapping ALL the fields in Customers, including the Address field. The following code works great:

    using (CSIntUnitOfWork uow = new CSIntUnitOfWork())
    {
        CustomerRepository customerRepository = new CustomerRepository(uow, _resellerID);
    
        DataModels.Customer updateCustomer = customerRepository.GetByID(customer.CustomerID);
    
        Mapper.CreateMap<Customer, Customer>()
            .ForMember(dest => dest.CustomerID, opt => opt.Ignore())
            .ForMember(dest => dest.Address, opt => opt.Ignore());  // <-- This was the problem!
        Mapper.Map(customer, updateCustomer);
    
        Mapper.CreateMap<Address, Address>()
            .ForMember(dest => dest.ID, opt => opt.Ignore());
        Mapper.Map(customer.Address, updateCustomer.Address);
    
        uow.SaveChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have one table called REVIEWS This table has Reviews that customers
In my project, I have a Customers table, with 3 foreign keys to the
I have a Customers table and would like to assign a Salesperson to each
Lets say I have a Customers table and an Orders table with a one-to-many
If I have a Customers table linked to an Orders table, and I want
This is the constraint I have on the Customers table. ALTER TABLE Customers ADD
I have a table Customers with a CustomerId field, and a table of Publications
I have a table of customers with a 1 recorded against their customerid on
In our DB (on SQL Server 2005) we have a Customers table, whose primary
I have an InnoDB table that lists customer orders - this table records the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.