automapper

How to configure AutoMapper for Polymorphism with explicit member mapping?

自闭症网瘾萝莉.ら 提交于 2019-12-19 08:04:33
问题 Consider the following basic case: Mapper.CreateMap<FromBase, ToBase>() .Include<FromD1, ToD1>() .Include<FromD2, ToD2>(); Mapper.CreateMap<FromD1, ToD1>() .ForMember( m => m.P0, a => a.MapFrom( x => x.Prop0 ) ) .ForMember( m => m.P1, a => a.MapFrom( x => x.Prop1 ) ); Mapper.CreateMap<FromD2, ToD2>() .ForMember( m => m.P0, a => a.MapFrom( x => x.Prop0 ) ) .ForMember( m => m.P2, a => a.MapFrom( x => x.Prop2 ) ); Mapper.AssertConfigurationIsValid(); FromBase[] froms = { new FromD1() { Prop0 =

AutoMapper many to many relationship into collection

冷暖自知 提交于 2019-12-19 07:44:00
问题 I'm trying to map entities into a collection through a many to many table using AutoMapper. My domain model (Entity Framework) looks like this: public class User { public int UserID { get; set; } public string Name { get; set; } public IList<UserCompany> UserCompanies { get; set; } } public class Company { public int CompanyID { get; set; } public string Name { get; set; } } public class UserCompany { public int UserCompanyID { get; set; } public int UserID { get; set; } public int CompanyID

AutoMapper many to many relationship into collection

一世执手 提交于 2019-12-19 07:43:55
问题 I'm trying to map entities into a collection through a many to many table using AutoMapper. My domain model (Entity Framework) looks like this: public class User { public int UserID { get; set; } public string Name { get; set; } public IList<UserCompany> UserCompanies { get; set; } } public class Company { public int CompanyID { get; set; } public string Name { get; set; } } public class UserCompany { public int UserCompanyID { get; set; } public int UserID { get; set; } public int CompanyID

automapper multi objects to one object

馋奶兔 提交于 2019-12-19 07:09:14
问题 I have two sub class that I need to copy a List element from into a master object public Class Foo1 : Anote { public bool Ison { get; set;} public List<Anote>Anotes { get; private set;} public Foo1() { this.Anotes = new List<Anote>(); } } public Class Foo2 : Bnote { public bool Ison { get; set;} public List<Bnote>Anotes { get; private set;} public Foo2() { this.Anotes = new List<Bnote>(); } } public Class Foo3 : Cnote { public bool Ison { get; set;} public List<Cnote>Anotes { get; private set

best way to project ViewModel back into Model

十年热恋 提交于 2019-12-19 06:18:26
问题 Consider having ViewModel : public class ViewModel { public int id {get;set;} public int a {get;set;} public int b {get;set;} } and a original Model like this: public class Model { public int id {get;set;} public int a {get;set;} public int b {get;set;} public int c {get;set;} public virtual Object d {get;set;} } Each time I get view model I have to put all ViewModel properties one by one into Model. Something like : var model = Db.Models.Find(viewModel.Id); model.a = viewModel.a; model.b =

Using AutoMapper to map unknown types

為{幸葍}努か 提交于 2019-12-19 05:35:09
问题 I'm using AutoMapper to copy the properties of one object to another: This is my code: // Get type and create first object Type itemType = Type.GetType(itemTypeName); var item = Activator.CreateInstance(itemType); // Set item properties .. Code removed for clarity .. // Get item from Entity Framework DbContext var set = dataContext.Set(itemType); var itemInDatabase = set.Find(id); if (itemInDatabase == null) { itemInDatabase = Activator.CreateInstance(itemType); set.Add(itemInDatabase); } //

Using AutoMapper to map unknown types

一世执手 提交于 2019-12-19 05:35:06
问题 I'm using AutoMapper to copy the properties of one object to another: This is my code: // Get type and create first object Type itemType = Type.GetType(itemTypeName); var item = Activator.CreateInstance(itemType); // Set item properties .. Code removed for clarity .. // Get item from Entity Framework DbContext var set = dataContext.Set(itemType); var itemInDatabase = set.Find(id); if (itemInDatabase == null) { itemInDatabase = Activator.CreateInstance(itemType); set.Add(itemInDatabase); } //

Using AutoMapper to map unknown types

五迷三道 提交于 2019-12-19 05:35:06
问题 I'm using AutoMapper to copy the properties of one object to another: This is my code: // Get type and create first object Type itemType = Type.GetType(itemTypeName); var item = Activator.CreateInstance(itemType); // Set item properties .. Code removed for clarity .. // Get item from Entity Framework DbContext var set = dataContext.Set(itemType); var itemInDatabase = set.Find(id); if (itemInDatabase == null) { itemInDatabase = Activator.CreateInstance(itemType); set.Add(itemInDatabase); } //

Map IList to ICollection through AutoMapper

邮差的信 提交于 2019-12-19 05:06:45
问题 public class Order { public int OrderId { get; set; } public string OrderCode { get; set; } public IList<OrderItem> OrderItems { get; set; } } public class OrderDTO { public string OrderId { get; set; } public string IdentifiableCode { get; set; } public decimal TotalCost { get; set; } public ICollection OrderItems { get; set; } } public class OrderItem { public int OrderItemId { get; set; } public int ItemCount { get; set; } public Order Order { get; set; } } public class OrderItemDTO {

Why does AutoMapper have an IValueFormatter when it has a seemingly much more powerful ValueResolver?

戏子无情 提交于 2019-12-18 18:55:38
问题 It looks like an IValueFormatter takes a value of type object and returns a value of type string , while a ValueResolver<TSource, TDestination> takes a value of any type and returns a value of any type. So, it's more flexible. There is also the matter that, with a ValueResolver , you never need to cast the source to a particular type--you define it explicitly in your class definition. Given this, why use IValueFormatter ? Does it do anything that can't be done with ValueResolver ? Am I