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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:51:32+00:00 2026-05-15T23:51:32+00:00

I’m using AutoMapper to map between a Linq Domain object and a ViewModel to

  • 0

I’m using AutoMapper to map between a Linq Domain object and a ViewModel to display an Edit Form to the user which works perfectly.

When they click submit I’d like to know the best way to map the ViewModel back to a Linq entity and persist it to the database.

The Linq entity I’m using has multiple collections of other entities (ie one-to-many references).

I was trying:

  1. build up my custom view model using UpdateModel
  2. get the previous state of the Linq entity by using the passed in id
  3. map the view model onto the Linq entity (using automapper)
  4. call SubmitChanges() on the data context

This method works when I’m only updating properties that are not collections, but throws errors when trying to modify properties that are collections.

Any help/thoughts would be much appreciated 🙂

  • 1 1 Answer
  • 1 View
  • 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-15T23:51:33+00:00Added an answer on May 15, 2026 at 11:51 pm

    I’ve taken an approach that is very similar to that used by Jimmy Bogard in the CodeCampServer project (http://codecampserver.codeplex.com/)

    I have a general Mapper class that I inherit from where I just need to override a MapToModel method that maps from the ViewModel to the domain Model, and a GetIdFromViewModel method that returns the proper Id form the ViewModel so that the Service layer can grab the domain model from the database.

    I had to change a little from the CodeCampServer examples because some of my Models used Guid and some used int as the Id for the Model.

    You should be able to grab the code from the codeplex link above and that should help get you going in that direction.

    Here is what one of my Mappers for a Member looks like:

    public class MemberMapper : AutoFormMapper<Member, MemberFormViewModel, Guid>, IMemberMapper
    {
        public MemberMapper(IMemberService service) : base(service) { }
    
        protected override Guid GetIdFromViewModel(MemberFormViewModel viewModel)
        {
            return viewModel.MemberId;
        }
    
        protected override void MapToModel(MemberFormViewModel viewModel, Member model)
        {
            // if the need arises, we will need to map the full objects as Foreign Key properties
            //  by using the proper repositories
            //  right now for loading the object to save back to the DB we don't have that need, so let's not waste the call
    
            model.MemberId = viewModel.MemberId;
            model.FirstName = viewModel.FirstName;
            model.LastName = viewModel.LastName;
            model.Title = viewModel.Title;
            model.EmailAddress = viewModel.EmailAddress;
            model.DirectPhone = viewModel.DirectPhone;
            model.MobilePhone = viewModel.MobilePhone;
            model.ElectronicId = viewModel.ElectronicId;
            model.ProjectRoleTypeId = viewModel.ProjectRoleTypeId;
            model.DepartmentId = viewModel.DepartmentId;
        }
    }
    

    Then you can use this MemberMapper to map both directions. It uses AutoMapper to go from the domain Model to the View Model and then uses the MapToModel method that you implement to map from the View Model back to the domain Model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.