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

  • Home
  • SEARCH
  • 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 7398031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:49:16+00:00 2026-05-29T03:49:16+00:00

Given the following entity model: public class Location { public int Id { get;

  • 0

Given the following entity model:

public class Location
{
    public int Id { get; set; }
    public Coordinates Center { get; set; }
}
public class Coordinates
{
    public double? Latitude { get; set; }
    public double? Longitude { get; set; }
}

… and the following view model:

public class LocationModel
{
    public int Id { get; set; }
    public double? CenterLatitude { get; set; }
    public double? CenterLongitude { get; set; }
}

The LocationModel properties are named such that mapping from the entity to the model does not require a custom resolver.

However when mapping from a model to an entity, the following custom resolver is needed:

CreateMap<LocationModel, Location>()
    .ForMember(target => target.Center, opt => opt
        .ResolveUsing(source => new Coordinates 
            { 
                Latitude = source.CenterLatitude, 
                Longitude = source.CenterLongitude 
            }))

Why is this? Is there a simpler way to make AutoMapper to construct a new Coordinates value object based on the naming conventions in the viewmodel?

Update

To answer the first comment, there is nothing special about the entity to viewmodel mapping:

CreateMap<Location, LocationModel>();
  • 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-29T03:49:17+00:00Added an answer on May 29, 2026 at 3:49 am

    Edit

    Please see comment thread below. This answer is actually for the opposite mapping.


    You’re doing something else wrong. You are following the convention correctly, so the mapping should work without any need for a resolver.

    I just tried this test, and it passed:

    public class Location
    {
        public int Id { get; set; }
        public Coordinates Center { get; set; }
    }
    
    public class Coordinates
    {
        public double? Latitude { get; set; }
        public double? Longitude { get; set; }
    }
    
    public class LocationModel
    {
        public int Id { get; set; }
        public double? CenterLatitude { get; set; }
        public double? CenterLongitude { get; set; }
    }
    
    [Test]
    public void LocationMapsToLocationModel()
    {
        Mapper.CreateMap<Location, LocationModel>();
    
        var location = new Location
        {
            Id = 1,
            Center = new Coordinates { Latitude = 1.11, Longitude = 2.22 }
        };
    
        var locationModel = Mapper.Map<LocationModel>(location);
    
        Assert.AreEqual(2.22, locationModel.CenterLongitude);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given domain model... public class Entity { public int Id { get; set; }
Given the following model: public class Foo { public int Id { get; set;}
Given the following entity- public class Friend { public virtual string Name { get;
given the following model example: @Entity @Table(name=tierenforcers) public class TierEnforcer extends Model { public
Given the following simplified model: public class Account { public Account() { Id =
I'm stuck with a query I need to write. Given the following model: public
Given the following Core Data Model: -> : to-one releationship ->>: to-many relationship Entity
Given the following entity definitions: @Entity class abstract A { Collection<A> parents; } @Entity
Consider the following Entity: @Entity public class Employee { @Id @GeneratedValue private Long id;
I have the following domain model: public class Campaign { public virtual long Id

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.