Last active
August 8, 2017 10:20
-
-
Save mahmut-gundogdu/e545ac1bd9818c400c0a486fd266f47a to your computer and use it in GitHub Desktop.
https://mahmutgundogdu.wordpress.com/?p=561 makale için gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using AutoMapper; | |
using AutoMapper.EquivalencyExpression; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
Mapper.Initialize(cfg => | |
{ | |
//Nuget ile AutoMapper.Collection yüklenmeli | |
cfg.AddCollectionMappers(); | |
cfg.CreateMap<MainModel, MainEntity>().ReverseMap(); | |
cfg.CreateMap<ChildModel, ChildEntity>().EqualityComparison((o, o2) => o.Id == o2.Id); | |
cfg.CreateMap<ChildEntity, ChildModel>().EqualityComparison((o, o2) => o.Id == o2.Id); | |
}); | |
MainEntity entity = new MainEntity() | |
{ | |
Id = 1, | |
Name = "Name1", | |
ChildClasses = new List<ChildEntity>() { new ChildEntity() { Id = 99, Address = "subName2" , MainClassId = 1} } | |
}; | |
MainModel model = new MainModel() | |
{ | |
Id = 1, | |
Name = "model Name1", | |
ChildClasses = new List<ChildModel>() { new ChildModel() {Id=99, Address = "model subName2" } } | |
}; | |
Mapper.Map(model, entity); | |
Console.WriteLine("Name: {0}",entity.Name); | |
Console.WriteLine("Address: {0}", entity.ChildClasses.First().Address); | |
Console.WriteLine("Id {0}",entity.ChildClasses.First().Id); | |
Console.WriteLine("MainClassId {0}", entity.ChildClasses.First().MainClassId); | |
Console.WriteLine("the MainClassId should be {0} and it is {1} .its Works!", 1, entity.ChildClasses.First().MainClassId); | |
} | |
} | |
public class MainEntity | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public virtual ICollection<ChildEntity> ChildClasses { get; set; } | |
} | |
public class ChildEntity | |
{ | |
public int Id { get; set; } | |
public string Address { get; set; } | |
public int MainClassId { get; set; } | |
} | |
public class MainModel | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public List<ChildModel> ChildClasses { get; set; } | |
} | |
public class ChildModel | |
{ | |
public int Id { get; set; } | |
public string Address { get; set; } | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using AutoMapper; | |
using AutoMapper.EquivalencyExpression; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
Mapper.Initialize(cfg => | |
{ | |
cfg.CreateMap<MainModel, MainEntity>().ReverseMap(); | |
cfg.CreateMap<ChildModel, ChildEntity>().ReverseMap(); | |
}); | |
MainEntity entity = new MainEntity() | |
{ | |
Id = 1, | |
Name = "Name1", | |
ChildClasses = new List<ChildEntity>() { new ChildEntity() { Id = 99, Address = "subName2" , MainClassId = 1} } | |
}; | |
MainModel model = new MainModel() | |
{ | |
Id = 1, | |
Name = "model Name1", | |
ChildClasses = new List<ChildModel>() { new ChildModel() {Id=99, Address = "model subName2" } } | |
}; | |
Mapper.Map(model, entity); | |
Console.WriteLine("Name: {0}",entity.Name); | |
Console.WriteLine("Address: {0}", entity.ChildClasses.First().Address); | |
Console.WriteLine("Id {0}",entity.ChildClasses.First().Id); | |
Console.WriteLine("MainClassId {0}", entity.ChildClasses.First().MainClassId); | |
Console.WriteLine("the MainClassId should be {0} but it is {1}!", 1, entity.ChildClasses.First().MainClassId); | |
} | |
} | |
public class MainEntity | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public virtual ICollection<ChildEntity> ChildClasses { get; set; } | |
} | |
public class ChildEntity | |
{ | |
public int Id { get; set; } | |
public string Address { get; set; } | |
public int MainClassId { get; set; } | |
} | |
public class MainModel | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public List<ChildModel> ChildClasses { get; set; } | |
} | |
public class ChildModel | |
{ | |
public int Id { get; set; } | |
public string Address { get; set; } | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment