Automapper not mapping all child entities within child collectionautomapper how to ignore property in source item that does not exist in destinationAutomapper: bidirectional mapping with ReverseMap() and ForMember()Automapper Mapping Non-null propertiesAutoMapper map object to Lookup field in MS Dynamics CRMMap readonly fields with AutomapperAutomapper parent-child self referencing loopSaving AutoMapper mapped Collections of Entities using Entity FrameworkAutomapper - Mapping Multiple Properties using 1 ResolverAutomapper fails to map two entitiesMake AutoMapper automatically map prefixed properties
Why was Mal so quick to drop Bester in favour of Kaylee?
Are the requirements of a Horn of Valhalla cumulative?
Why would anyone even use a Portkey?
Just graduated with a master’s degree, but I internalised nothing
Who voices the character "Finger" in The Fifth Element?
How to properly say asset/assets in German
How do we separate rules of logic from non-logical constraints?
What are good ways to spray paint a QR code on a footpath?
What's the safest way to inform a new user of their password on an invite-only website?
Closest Proximity of Oceans to Freshwater Springs
How did installing this RPM create a file?
What verb for taking advantage fits in "I don't want to ________ on the friendship"?
Is there reliable evidence that depleted uranium from the 1999 NATO bombing is causing cancer in Serbia?
What will happen if I checked in for another room in the same hotel, but not for the booked one?
Using “ser” without "un/una"?
Can you actually break an FPGA by programming it wrong?
Why wasn't ASCII designed with a contiguous alphanumeric character order?
How did researchers find articles before the Internet and the computer era?
Golf the smallest circle!
Was it really unprofessional of me to leave without asking for a raise first?
How to describe POV characters?
Sacrifice blocking creature before damage is dealt no longer working (MtG Arena)?
Are all commands with an optional argument fragile?
Can one use the present progressive or gerund like an adjective?
Automapper not mapping all child entities within child collection
automapper how to ignore property in source item that does not exist in destinationAutomapper: bidirectional mapping with ReverseMap() and ForMember()Automapper Mapping Non-null propertiesAutoMapper map object to Lookup field in MS Dynamics CRMMap readonly fields with AutomapperAutomapper parent-child self referencing loopSaving AutoMapper mapped Collections of Entities using Entity FrameworkAutomapper - Mapping Multiple Properties using 1 ResolverAutomapper fails to map two entitiesMake AutoMapper automatically map prefixed properties
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am having a problem trying to get my entities mapped using Automapper.
I already have the following mappings where 'GearLevel' has a collection of 'Gear' and there will always be 6.
When i map the entities, if the 'Gear' has multiple entities with the same name it only maps one of them so i will only get one of those entities mapped.
Should i be doing something else in my mappings to overcome this and if so how do i configure it?
CreateMap<HeroViewModel, Hero>();
CreateMap<TagViewModel, Tag>();
CreateMap<AbilityViewModel, Ability>();
CreateMap<StatViewModel, Stat>();
CreateMap<GearLevelViewModel, GearLevel>();
CreateMap<GearViewModel, Gear>();
CreateMap<HeroViewModel, RegisterNewHeroCommand>()
.ConstructUsing(c => new RegisterNewHeroCommand(c.Name, c.Damage))
.ForMember(dest => dest.Tags, opt => opt.MapFrom(src => src.Tags))
.ForMember(dest => dest.Abilities, opt => opt.MapFrom(src => src.Abilities))
.ForMember(dest => dest.Stats, opt => opt.MapFrom(src => src.Stats))
.ForMember(dest => dest.GearLevels, opt => opt.MapFrom(src => src.GearLevels));
I am calling the AutoMapper in the following code and when it calls the mapper the 'viewModel' has all the 'Gear' in the 'GearLevel' but when the 'registerCommand' returns it is missing multiple 'Gear' in the 'GearLevel' with the same name.
public async Task Register(HeroViewModel viewModel)
try
var registerCommand = _mapper.Map<RegisterNewHeroCommand>(viewModel);
await _mediator.SendCommand(registerCommand);
catch (Exception exception)
_logger.LogError(exception, $"Failed to register 'viewModel.Name'");
This uses the following entities:
public class HeroViewModel
public Guid Id get; set;
public string Name get; set;
public int? Damage get; set;
public List<TagViewModel> Tags get; set;
public List<AbilityViewModel> Abilities get; set;
public List<StatViewModel> Stats get; set;
public List<GearLevelViewModel> GearLevels get; set;
public class GearLevelViewModel
public Guid Id get; set;
public int? Level get; set;
public List<GearViewModel> Gear get; set;
public class GearViewModel
public Guid Id get; set;
public string Name get; set;
And the following commands:
public class RegisterNewHeroCommand : HeroCommand
public RegisterNewHeroCommand(string name, int? damage)
Name = name;
Damage = damage;
public abstract class HeroCommand : Command
public Guid Id get; protected set;
public string Name get; protected set;
public int? Damage get; protected set;
public List<Tag> Tags get; protected set;
public List<Ability> Abilities get; protected set;
public List<Stat> Stats get; protected set;
public List<GearLevel> GearLevels get; protected set;
c# automapper
|
show 2 more comments
I am having a problem trying to get my entities mapped using Automapper.
I already have the following mappings where 'GearLevel' has a collection of 'Gear' and there will always be 6.
When i map the entities, if the 'Gear' has multiple entities with the same name it only maps one of them so i will only get one of those entities mapped.
Should i be doing something else in my mappings to overcome this and if so how do i configure it?
CreateMap<HeroViewModel, Hero>();
CreateMap<TagViewModel, Tag>();
CreateMap<AbilityViewModel, Ability>();
CreateMap<StatViewModel, Stat>();
CreateMap<GearLevelViewModel, GearLevel>();
CreateMap<GearViewModel, Gear>();
CreateMap<HeroViewModel, RegisterNewHeroCommand>()
.ConstructUsing(c => new RegisterNewHeroCommand(c.Name, c.Damage))
.ForMember(dest => dest.Tags, opt => opt.MapFrom(src => src.Tags))
.ForMember(dest => dest.Abilities, opt => opt.MapFrom(src => src.Abilities))
.ForMember(dest => dest.Stats, opt => opt.MapFrom(src => src.Stats))
.ForMember(dest => dest.GearLevels, opt => opt.MapFrom(src => src.GearLevels));
I am calling the AutoMapper in the following code and when it calls the mapper the 'viewModel' has all the 'Gear' in the 'GearLevel' but when the 'registerCommand' returns it is missing multiple 'Gear' in the 'GearLevel' with the same name.
public async Task Register(HeroViewModel viewModel)
try
var registerCommand = _mapper.Map<RegisterNewHeroCommand>(viewModel);
await _mediator.SendCommand(registerCommand);
catch (Exception exception)
_logger.LogError(exception, $"Failed to register 'viewModel.Name'");
This uses the following entities:
public class HeroViewModel
public Guid Id get; set;
public string Name get; set;
public int? Damage get; set;
public List<TagViewModel> Tags get; set;
public List<AbilityViewModel> Abilities get; set;
public List<StatViewModel> Stats get; set;
public List<GearLevelViewModel> GearLevels get; set;
public class GearLevelViewModel
public Guid Id get; set;
public int? Level get; set;
public List<GearViewModel> Gear get; set;
public class GearViewModel
public Guid Id get; set;
public string Name get; set;
And the following commands:
public class RegisterNewHeroCommand : HeroCommand
public RegisterNewHeroCommand(string name, int? damage)
Name = name;
Damage = damage;
public abstract class HeroCommand : Command
public Guid Id get; protected set;
public string Name get; protected set;
public int? Damage get; protected set;
public List<Tag> Tags get; protected set;
public List<Ability> Abilities get; protected set;
public List<Stat> Stats get; protected set;
public List<GearLevel> GearLevels get; protected set;
c# automapper
Can you add some (minimal) example code showing how it fails?
– stuartd
Mar 25 at 14:14
The actual code is working and i am getting the 'GearLevel' collection fully mapped, but the child collection of 'Gear' is not. What code do you need and i can post it?
– mat_e3
Mar 25 at 14:25
Ideally a minimal reproducible example of the problem - so the simplest object model which exhibits the problem with some sample data. You should be able to fit that into your question.
– stuartd
Mar 25 at 14:32
I have added some more detail in the original question
– mat_e3
Mar 25 at 14:47
I'd suggest you should simply remove all theMapFromcalls. It should work theoretically because the names are the same. If that helps I'll put that as the answer
– Andrey Stukalin
Mar 25 at 16:28
|
show 2 more comments
I am having a problem trying to get my entities mapped using Automapper.
I already have the following mappings where 'GearLevel' has a collection of 'Gear' and there will always be 6.
When i map the entities, if the 'Gear' has multiple entities with the same name it only maps one of them so i will only get one of those entities mapped.
Should i be doing something else in my mappings to overcome this and if so how do i configure it?
CreateMap<HeroViewModel, Hero>();
CreateMap<TagViewModel, Tag>();
CreateMap<AbilityViewModel, Ability>();
CreateMap<StatViewModel, Stat>();
CreateMap<GearLevelViewModel, GearLevel>();
CreateMap<GearViewModel, Gear>();
CreateMap<HeroViewModel, RegisterNewHeroCommand>()
.ConstructUsing(c => new RegisterNewHeroCommand(c.Name, c.Damage))
.ForMember(dest => dest.Tags, opt => opt.MapFrom(src => src.Tags))
.ForMember(dest => dest.Abilities, opt => opt.MapFrom(src => src.Abilities))
.ForMember(dest => dest.Stats, opt => opt.MapFrom(src => src.Stats))
.ForMember(dest => dest.GearLevels, opt => opt.MapFrom(src => src.GearLevels));
I am calling the AutoMapper in the following code and when it calls the mapper the 'viewModel' has all the 'Gear' in the 'GearLevel' but when the 'registerCommand' returns it is missing multiple 'Gear' in the 'GearLevel' with the same name.
public async Task Register(HeroViewModel viewModel)
try
var registerCommand = _mapper.Map<RegisterNewHeroCommand>(viewModel);
await _mediator.SendCommand(registerCommand);
catch (Exception exception)
_logger.LogError(exception, $"Failed to register 'viewModel.Name'");
This uses the following entities:
public class HeroViewModel
public Guid Id get; set;
public string Name get; set;
public int? Damage get; set;
public List<TagViewModel> Tags get; set;
public List<AbilityViewModel> Abilities get; set;
public List<StatViewModel> Stats get; set;
public List<GearLevelViewModel> GearLevels get; set;
public class GearLevelViewModel
public Guid Id get; set;
public int? Level get; set;
public List<GearViewModel> Gear get; set;
public class GearViewModel
public Guid Id get; set;
public string Name get; set;
And the following commands:
public class RegisterNewHeroCommand : HeroCommand
public RegisterNewHeroCommand(string name, int? damage)
Name = name;
Damage = damage;
public abstract class HeroCommand : Command
public Guid Id get; protected set;
public string Name get; protected set;
public int? Damage get; protected set;
public List<Tag> Tags get; protected set;
public List<Ability> Abilities get; protected set;
public List<Stat> Stats get; protected set;
public List<GearLevel> GearLevels get; protected set;
c# automapper
I am having a problem trying to get my entities mapped using Automapper.
I already have the following mappings where 'GearLevel' has a collection of 'Gear' and there will always be 6.
When i map the entities, if the 'Gear' has multiple entities with the same name it only maps one of them so i will only get one of those entities mapped.
Should i be doing something else in my mappings to overcome this and if so how do i configure it?
CreateMap<HeroViewModel, Hero>();
CreateMap<TagViewModel, Tag>();
CreateMap<AbilityViewModel, Ability>();
CreateMap<StatViewModel, Stat>();
CreateMap<GearLevelViewModel, GearLevel>();
CreateMap<GearViewModel, Gear>();
CreateMap<HeroViewModel, RegisterNewHeroCommand>()
.ConstructUsing(c => new RegisterNewHeroCommand(c.Name, c.Damage))
.ForMember(dest => dest.Tags, opt => opt.MapFrom(src => src.Tags))
.ForMember(dest => dest.Abilities, opt => opt.MapFrom(src => src.Abilities))
.ForMember(dest => dest.Stats, opt => opt.MapFrom(src => src.Stats))
.ForMember(dest => dest.GearLevels, opt => opt.MapFrom(src => src.GearLevels));
I am calling the AutoMapper in the following code and when it calls the mapper the 'viewModel' has all the 'Gear' in the 'GearLevel' but when the 'registerCommand' returns it is missing multiple 'Gear' in the 'GearLevel' with the same name.
public async Task Register(HeroViewModel viewModel)
try
var registerCommand = _mapper.Map<RegisterNewHeroCommand>(viewModel);
await _mediator.SendCommand(registerCommand);
catch (Exception exception)
_logger.LogError(exception, $"Failed to register 'viewModel.Name'");
This uses the following entities:
public class HeroViewModel
public Guid Id get; set;
public string Name get; set;
public int? Damage get; set;
public List<TagViewModel> Tags get; set;
public List<AbilityViewModel> Abilities get; set;
public List<StatViewModel> Stats get; set;
public List<GearLevelViewModel> GearLevels get; set;
public class GearLevelViewModel
public Guid Id get; set;
public int? Level get; set;
public List<GearViewModel> Gear get; set;
public class GearViewModel
public Guid Id get; set;
public string Name get; set;
And the following commands:
public class RegisterNewHeroCommand : HeroCommand
public RegisterNewHeroCommand(string name, int? damage)
Name = name;
Damage = damage;
public abstract class HeroCommand : Command
public Guid Id get; protected set;
public string Name get; protected set;
public int? Damage get; protected set;
public List<Tag> Tags get; protected set;
public List<Ability> Abilities get; protected set;
public List<Stat> Stats get; protected set;
public List<GearLevel> GearLevels get; protected set;
c# automapper
c# automapper
edited Mar 25 at 14:47
mat_e3
asked Mar 25 at 14:09
mat_e3mat_e3
266 bronze badges
266 bronze badges
Can you add some (minimal) example code showing how it fails?
– stuartd
Mar 25 at 14:14
The actual code is working and i am getting the 'GearLevel' collection fully mapped, but the child collection of 'Gear' is not. What code do you need and i can post it?
– mat_e3
Mar 25 at 14:25
Ideally a minimal reproducible example of the problem - so the simplest object model which exhibits the problem with some sample data. You should be able to fit that into your question.
– stuartd
Mar 25 at 14:32
I have added some more detail in the original question
– mat_e3
Mar 25 at 14:47
I'd suggest you should simply remove all theMapFromcalls. It should work theoretically because the names are the same. If that helps I'll put that as the answer
– Andrey Stukalin
Mar 25 at 16:28
|
show 2 more comments
Can you add some (minimal) example code showing how it fails?
– stuartd
Mar 25 at 14:14
The actual code is working and i am getting the 'GearLevel' collection fully mapped, but the child collection of 'Gear' is not. What code do you need and i can post it?
– mat_e3
Mar 25 at 14:25
Ideally a minimal reproducible example of the problem - so the simplest object model which exhibits the problem with some sample data. You should be able to fit that into your question.
– stuartd
Mar 25 at 14:32
I have added some more detail in the original question
– mat_e3
Mar 25 at 14:47
I'd suggest you should simply remove all theMapFromcalls. It should work theoretically because the names are the same. If that helps I'll put that as the answer
– Andrey Stukalin
Mar 25 at 16:28
Can you add some (minimal) example code showing how it fails?
– stuartd
Mar 25 at 14:14
Can you add some (minimal) example code showing how it fails?
– stuartd
Mar 25 at 14:14
The actual code is working and i am getting the 'GearLevel' collection fully mapped, but the child collection of 'Gear' is not. What code do you need and i can post it?
– mat_e3
Mar 25 at 14:25
The actual code is working and i am getting the 'GearLevel' collection fully mapped, but the child collection of 'Gear' is not. What code do you need and i can post it?
– mat_e3
Mar 25 at 14:25
Ideally a minimal reproducible example of the problem - so the simplest object model which exhibits the problem with some sample data. You should be able to fit that into your question.
– stuartd
Mar 25 at 14:32
Ideally a minimal reproducible example of the problem - so the simplest object model which exhibits the problem with some sample data. You should be able to fit that into your question.
– stuartd
Mar 25 at 14:32
I have added some more detail in the original question
– mat_e3
Mar 25 at 14:47
I have added some more detail in the original question
– mat_e3
Mar 25 at 14:47
I'd suggest you should simply remove all the
MapFrom calls. It should work theoretically because the names are the same. If that helps I'll put that as the answer– Andrey Stukalin
Mar 25 at 16:28
I'd suggest you should simply remove all the
MapFrom calls. It should work theoretically because the names are the same. If that helps I'll put that as the answer– Andrey Stukalin
Mar 25 at 16:28
|
show 2 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55339722%2fautomapper-not-mapping-all-child-entities-within-child-collection%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55339722%2fautomapper-not-mapping-all-child-entities-within-child-collection%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you add some (minimal) example code showing how it fails?
– stuartd
Mar 25 at 14:14
The actual code is working and i am getting the 'GearLevel' collection fully mapped, but the child collection of 'Gear' is not. What code do you need and i can post it?
– mat_e3
Mar 25 at 14:25
Ideally a minimal reproducible example of the problem - so the simplest object model which exhibits the problem with some sample data. You should be able to fit that into your question.
– stuartd
Mar 25 at 14:32
I have added some more detail in the original question
– mat_e3
Mar 25 at 14:47
I'd suggest you should simply remove all the
MapFromcalls. It should work theoretically because the names are the same. If that helps I'll put that as the answer– Andrey Stukalin
Mar 25 at 16:28