automapper

AutoMapper: Collection to Single string Property

别等时光非礼了梦想. 提交于 2019-12-17 20:47:37
问题 I have a scenario in which I have to do following mapping public class Company : BaseEntity { public string Name { get; set; } public virtual ICollection<CompanyService> CompanyServices { get; set; } } public class Service : BaseEntity { public string Name { get; set; } public virtual ICollection<CompanyService> CompanyServices { get; set; } } public class CompanyService : BaseEntity { public long CompanyID { get; set; } public virtual Company Company { get; set; } public long ServiceID { get

AutoMapper mapping properties with private setters

孤者浪人 提交于 2019-12-17 20:03:55
问题 Is it possible to assign properties with private setters using AutoMapper? 回答1: If you set the value for this properties in the constructor like this public class RestrictedName { public RestrictedName(string name) { Name = name; } public string Name { get; private set; } } public class OpenName { public string Name { get; set; } } then you can use ConstructUsing like this Mapper.CreateMap<OpenName, RestrictedName>() .ConstructUsing(s => new RestrictedName(s.Name)); which works with this code

How to add AutoMapper to DI in .Net core 2.0 Console Application

邮差的信 提交于 2019-12-17 17:14:17
问题 I try to use AutoMapper in my .Net Core 2.0 Console Application. My class looks like this: public class AutoGetCurrency { private readonly IMapper mapper; public AutoGetCurrency(IMapper mapper) { this.mapper = mapper; } .... } I tried to add services into Main method: static void Main(string[] args) { var services = new ServiceCollection(); ServiceProvider = services.BuildServiceProvider(); services.AddTransient<IMapper>(); .... } But I have the next error 'One or more errors occurred.

Ignore mapping one property with Automapper

我的未来我决定 提交于 2019-12-17 16:43:18
问题 I'm using Automapper and I have the following scenario: Class OrderModel has a property called 'ProductName' that isn't in the database. So when I try to do the mapping with: Mapper.CreateMap<OrderModel, Orders>(); It generates an exception : "The following 1 properties on Project.ViewModels.OrderModel are not mapped: 'ProductName' I've read at AutoMapper's Wiki for Projections the opposite case (the extra attribute is on the destination, not in the source which is actually my case ) How can

Automapper:Converting JSON to list of objects

余生颓废 提交于 2019-12-17 16:23:04
问题 Source Object (JSON, using JSON.NET if it matters): { "conum" : 1001, "name" : "CLN Industries Corporation", "agencyName" : "Murphy, Holmes & Associates, LLC", "sAA" : [{ "code" : 247, "description" : "Mechanic\u0027s lien - Bond to Discharge - Fixed penalty - where principal has posted Performance and Pa" }, { "code" : 277, "description" : "Mechanic\u0027s lien - Bond to Discharge - Open Penalty - where principal has posted Performance and Paym" }, { "code" : 505, "description" : "Indemnity

Mapping a DTO to an Entity with Automapper

心不动则不痛 提交于 2019-12-17 15:46:41
问题 I have an Entity Framework POCO with the following structure. public class Entity { public virtual int Id { get; set; } public virtual string Name { get; set; } } I've created a Data Transfer Object for this entity to be used by my views. public class EntityDto { public int Id { get; set; } public string Name { get; set; } } Now, I have the following mapping code in my Global.asax file. Mapper.CreateMap<Entity, EntityDto>(); Mapper.CreateMap<EntityDto, Entity>(); // not sure whether I need

How do you map a Dto to an existing object instance with nested objects using AutoMapper?

喜欢而已 提交于 2019-12-17 10:50:12
问题 I have the following Dto and entity with a nested sub entity. public class Dto { public string Property { get; set; } public string SubProperty { get; set; } } public class Entity { public string Property { get; set; } public SubEntity Sub { get; set; } } public class SubEntity { public string SubProperty { get; set; } } How can I set up a mapping with AutoMapper that will allow me to update an existing instance of Entity with the values from a Dto . I'm using Mapper.Map(dto, entity) to

Allow mapping of dynamic types using AutoMapper or similar?

泄露秘密 提交于 2019-12-17 10:12:31
问题 I've started to use https://github.com/robconery/massive for a project, I wonder if there is any mapping tool that allows support for Dynamic to static type mapping? I've used AutoMapper previously, does AutoMapper support this? I am aware of the DynamicMap function from AutoMapper, however I believe this function is for running maps without creating the Map first. In my example below it does not work. dynamic curUser = users.GetSingleUser(UserID); var retUser = Mapper.DynamicMap<UserModel>

Automapper: Ignore on condition of

心已入冬 提交于 2019-12-17 07:52:07
问题 Is it possible to ignore mapping a member depending on the value of a source property? For example if we have: public class Car { public int Id { get; set; } public string Code { get; set; } } public class CarViewModel { public int Id { get; set; } public string Code { get; set; } } I'm looking for something like Mapper.CreateMap<CarViewModel, Car>() .ForMember(dest => dest.Code, opt => opt.Ignore().If(source => source.Id == 0)) So far the only solution I have is too use two different view

AutoMapper: What is the difference between MapFrom and ResolveUsing?

北城以北 提交于 2019-12-17 07:26:55
问题 Ignoring the ResolveUsing overloads that take an IValueResolver, and looking only at these 2 methods: void ResolveUsing(Func<TSource, object> resolver); void MapFrom<TMember>(Expression<Func<TSource, TMember>> sourceMember); The main difference between these 2 seems to be that ResolveUsing takes a Func<TSource, object> , whereas MapFrom takes an Expression<Func<TSource, TMember>> . However in client code that actually uses one of these methods with a lambda expression, they seem to be