Note that there are some explanatory texts on larger screens.

plurals
  1. POAutoMapper: Mapping between the two collections with 'Ignore'
    primarykey
    data
    text
    <p>I'm using AutoMapper to map between the two collections. What I see is that the option <code>Ignore</code> is not working at this scenario as expected. What i'm expecting can be see in the method <code>AutoMapperIgnore_TwoObjectMappedWithIgnoreId_SameWithUnchangedIdAndNewPrice()</code>. In other two test methods is the <code>Id</code> although ignored but every object in the collection is created once again with the consequences that original values are going to be lost. There is possibility to use <code>UseDestinationValue</code>, but I think this make only sense on the classes where collection are members of it. How can I use the option <code>Ignore</code> on the collections?</p> <pre><code>[TestClass] public class AutoMapperTests { private readonly IEnumerable&lt;Dto&gt; _testDtos; private readonly IEnumerable&lt;Entity&gt; _testEntities; public AutoMapperTests() { _testDtos = new List&lt;Dto&gt; { new Dto() { Id = 0, Fk_Id = 8, Price = 350000 } }; _testEntities = new List&lt;Entity&gt; { new Entity() { Id = 8, Price = 68000 } , new Entity() { Id = 6, Price = 350000 } }; } [TestInitialize] public void TestInitialize() { Mapper.Reset(); } [TestMethod] public void AutoMapperIgnore_TwoCollectionsWithOneCommonElementMappedFromDtoToEntityIgnoreId_SameWithUnchangedIdAndNewPrice() { //Assign Mapper.CreateMap&lt;Dto, Entity&gt;() .ForMember(destination =&gt; destination.Id, opt =&gt; opt.Ignore()); AutoMapperIgnore_TwoCollectionsWithOneCommonElementMappedFromDtoToEntity_SameWithUnchangedIdAndNewPrice(true); } [TestMethod] public void AutoMapperIgnore_TwoCollectionsWithOneCommonElementMappedFromDtoToEntityUseDestinationtValueForId_SameWithUnchangedIdAndNewPrice() { //Assign Mapper.CreateMap&lt;Dto, Entity&gt;() .ForMember(destination =&gt; destination.Id, opt =&gt; opt.UseDestinationValue()); AutoMapperIgnore_TwoCollectionsWithOneCommonElementMappedFromDtoToEntity_SameWithUnchangedIdAndNewPrice(true); } private void AutoMapperIgnore_TwoCollectionsWithOneCommonElementMappedFromDtoToEntity_SameWithUnchangedIdAndNewPrice(bool isExceptedSame) { //Assign var exceptedPrice = _testDtos.First().Price; //Act IEnumerable&lt;Entity&gt; foundEntities = _testEntities.Join(_testDtos, e =&gt; e.Id, e =&gt; e.Fk_Id, (entity, dto) =&gt; entity).ToList(); Entity entityBeforeMapping = foundEntities.First(); Mapper.Map(_testDtos, foundEntities); Entity entityAfterMapping = foundEntities.First(); //Assert if (isExceptedSame) { Assert.AreSame(entityBeforeMapping, entityAfterMapping); } Assert.AreEqual(entityBeforeMapping.Id, entityAfterMapping.Id); Assert.AreEqual(exceptedPrice, entityAfterMapping.Price); } [TestMethod] public void AutoMapperIgnore_TwoObjectMappedWithIgnoreId_SameWithUnchangedIdAndNewPrice() { //Assign Mapper.CreateMap&lt;Dto, Entity&gt;() .ForMember(destination =&gt; destination.Id, opt =&gt; opt.Ignore()); var testDto = new Dto() { Id = 0, Fk_Id = 8, Price = 350000 }; var testEntity = new Entity() { Id = 8, Price = 68000 }; var exceptedPrice = testDto.Price; //Act Entity entityBeforeMapping = testEntity; Mapper.Map(testDto, testEntity); Entity entityAfterMapping = testEntity; //Assert Assert.AreSame(entityBeforeMapping, entityAfterMapping); Assert.AreEqual(entityBeforeMapping.Id, entityAfterMapping.Id); Assert.AreEqual(exceptedPrice, entityAfterMapping.Price); } internal class Dto { public int Id { get; set; } public int Fk_Id { get; set; } public Single? Price { get; set; } } internal class Entity { public int Id { get; set; } public Single? Price { get; set; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload