Skip to content

Instantly share code, notes, and snippets.

@StillLearnin
Last active November 23, 2018 21:26
Show Gist options
  • Save StillLearnin/754108825f82647e8f2e85bb073e34bf to your computer and use it in GitHub Desktop.
Save StillLearnin/754108825f82647e8f2e85bb073e34bf to your computer and use it in GitHub Desktop.
using AutoMapper;
using System;
using System.Collections.Generic;
namespace AutoMapper_Repro
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Person, PersonDTO>()
.ReverseMap()
.ForMember(props => props.Siblings, opts => opts.Ignore());
cfg.CreateMap<Family, FamilyDTO>()
.ReverseMap();
});
var familyDTO = new FamilyDTO();
var family = new Family
{
Father = new Person { Name = "Dad" },
Mother = new Person { Name = "Mom" },
Children = new List<Person>
{
new Person { Name = "Bill",
Siblings = new List<Person> {
new Person { Name = "Jimmy" },
new Person { Name = "Sue" }
}
}
}
};
Mapper.Map(family, familyDTO);
familyDTO.Children[0].Name = "William";
Mapper.Map(familyDTO, family);
throw new Exception("William (Bill) no longer has siblings!");
}
}
class PersonDTO
{
public string Name { get; set; }
}
class Person
{
public string Name { get; set; }
public List<Person> Siblings { get; set; }
}
class FamilyDTO
{
public PersonDTO Father { get; set; }
public PersonDTO Mother { get; set; }
public List<PersonDTO> Children { get; set; }
}
class Family
{
public Person Father { get; set; }
public Person Mother { get; set; }
public List<Person> Children { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment