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 7513193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:06:05+00:00 2026-05-30T00:06:05+00:00

When I execute the code below, in address on Mapper.Map line it’s ok I

  • 0

When I execute the code below, in address on Mapper.Map line it’s ok I have the right values coming from the model but the customer.Address, an ISet collection, is not updated on session.Save(customer) line. Should be updated since address is a reference.

public ActionResult SaveAddressInvoice(CustomerAddressForView model)
{
    var tx = session.BeginTransaction();

    var customer = session.Get<Customer>(customerId);
    var address = customer.Address.Where(x => x.Id == myAddressId).First<CustomerAddress>();

    address = Mapper.Map<CustomerAddressForView, CustomerAddress>(model);

    session.Save(customer);
    tx.Commit();
}

If I do:

var address = customer.Address.Where(x => x.Id == myAddressId).First<CustomerAddress>();
address.Street = "MyStreet";

I see the entry changed in the collection.

The configuration mapping is:

Mapper.CreateMap<CustomerAddressForView, CustomerAddress>()
    .ForMember(x => x.Id, opt => opt.Ignore());

Any idea?

Update 1

public class Customer
{
    public virtual int Id { get; set; }
    public virtual string LastName { get; set; }
    public virtual Iesi.Collections.Generic.ISet<CustomerAddress> Address { get; set; }

    public Customer()
    {
        Address = new Iesi.Collections.Generic.HashedSet<CustomerAddress>(); 
    }
}

public class CustomerAddress
{
    public virtual int Id { get; set; }
    public virtual string Street { get; set; }
    public virtual Customer Customer { get; set; }
}
  • 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-30T00:06:07+00:00Added an answer on May 30, 2026 at 12:06 am

    The reason why NHibernate isn’t updating the CustomerAddress object referenced by customer.Addresses is because the address variable gets overwritten with a new object in the call to the Mapper.Map method:

    address = Mapper.Map<CustomerAddressForView, CustomerAddress>(model);
    

    AutoMapper creates a new CustomerAddress object, which isn’t associated to the retrieved Customer, hence nothing gets updated when you call session.Save().

    You need to pass a reference to the retrieved CustomerAddress object to AutoMapper in order to update its properties:

    var address = customer.Address
        .Where(x => x.Id == myAddressId)
        .First<CustomerAddress>();
    Mapper.Map(model, address); // Updates the existing address
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to execute JavaScript code from within a C# assembly and have the
I have a simple QT object. When I execute the code below the control
The code below shows one email address as a output but I want to
HI I wanna timeout session from client page,i tried below code but not able
When I execute the code saveXML below it generates the error above, why?? using
I get the following error when I try and execute the code down below.
I have a form with a few buttons which execute code when pressed like
I have an event handler that needs to determine a type and execute code
I want to execute python code from C# with following code. static void Main(string[]
When I execute the code below, I get the common exception The process cannot

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.