automapper

Automapper: Mapping onto existing nested complex property

大兔子大兔子 提交于 2019-12-13 06:16:34
问题 Is it possible to tell AutoMapper during map creating to map onto existing instance of nested property? Let's suppose I've got a class: public class SomeClass { public int Id {get; set;} public Complex Settings {get; set;} } public class Complex { public int Id { get; set;} public string SomeText { get; set;} } I want to create map from SomeClass to SomeClass and use it to map properties onto existing instance. Mapper.CreateMap<SomeClass, SomeClass>() .ForMember(src => src.Settings, opts =>

Generic mapping with Automapper

て烟熏妆下的殇ゞ 提交于 2019-12-13 05:23:41
问题 I would like to create a generic/common mapping that will be the equivalent of the following mappings but with single line. cfg.CreateMap<int?, PropertyModel<int?>>().ConvertUsing<ValueToPropertyModelConverter<int?>>(); cfg.CreateMap<double?, PropertyModel<double?>>().ConvertUsing<ValueToPropertyModelConverter<double?>>(); cfg.CreateMap<string, PropertyModel<string>>().ConvertUsing<ValueToPropertyModelConverter<string>>(); I would imagine something like cfg.CreateMap<TValue, PropertyModel

c# automapper nested object to flat object list

六眼飞鱼酱① 提交于 2019-12-13 05:06:06
问题 I have the following example structure: public class Client { public Guid Id { get; set; } public string Name { get; set; } public ICollection<Adress> AdressList { get; set; } } public class Adress { public Guid Id { get; set; } public string street { get; set; } } I want to automap this structure to something what normalizr does for javascript. I want to have a result that looks like this object public class ViewModelRoot { public ICollection<ViewModelClient> ViewModelClientList { get; set;

Inject ISession into custom valueresolver

孤人 提交于 2019-12-13 04:32:33
问题 I'm trying to inject an instance of ISession into a custom AutoMapper ValueResolver . Here's the resolver public class ContactTypeResolver : ValueResolver<Common.Models.ContactType, Models.ContactType> { ISession _session; public ContactTypeResolver(ISession session) { _session = session; } protected override Models.ContactType ResolveCore(Common.Models.ContactType source) { return _session.Load<Models.ContactType>(source.Id); } } I have a profile for setting up AutoMapper this.CreateMap

AutoMapper ProjectTo() configuration question

落爺英雄遲暮 提交于 2019-12-13 04:29:57
问题 Not able to map one of the Dto's: ViewModel: public class TicketViewModel { public TicketDto Ticket { get; set; } public CompanyDto Company { get; set; } public TicketStateDto TicketState { get; set; } public string TicketPriorityName { get; set; } } Company and TicketState are navigation properties. Query: var query = _ticketRepository.GetAll() // return IQueryable<Ticket> .Include(c=> c.Company) .Include(tt => tt.TicketState) .Include(ts => ts.TicketPriority) .OrderBy(n => n.Id) .ProjectTo

Mapping a model into a dto and 'include' only the pair <id,string> of child element

拈花ヽ惹草 提交于 2019-12-13 04:21:56
问题 I received a nice implementation form mapping the key (ID) of a nested child (see here: Mapping a model into a dto and 'include' only the key of child element) but here I search for a slightly different solution as explained below. I have the following Project model: public class Project { [Key] public int ProjectID { get; set; } public string Name { get; set; } public string Description { get; set; } public virtual ICollection<Screenshot> Screenshots { get; set; } } And I have the following

Strange behavior of Automapper when property type is ignored

你离开我真会死。 提交于 2019-12-13 04:12:01
问题 After I saw how to ignore a property type with Automapper, I have tried it in a test project. It turns out that the property of a specific type is ignored properly, but when invoking AssertConfigurationIsValid() an exception is thrown, specifying that unmapped members were found. I can understand the reasoning of this exception, since the members of the type which should be ignored are not mapped, but what I am wondering is if this exceptions should be thrown in the context where I removed a

Read Claims from Automapper Value Resolver on NET Core

天大地大妈咪最大 提交于 2019-12-13 03:57:41
问题 I have a Automapper Mapping Profile like this: CreateMap<MyViewModel, MyDto>() .ForMember(s => s.MyProperty, opt => opt.ResolveUsing<CustomResolver>()); And this is my CustomResolver class (that aims to solve a value through the Claims): public class CustomResolver : IValueResolver<object, object, string> { private readonly HttpContext _context; public CompanyResolver(IHttpContextAccessor httpContextAccessor) { _context = httpContextAccessor.HttpContext; } public string Resolve(object source,

How to use AutoMapper to map objects by order defined in the class?

巧了我就是萌 提交于 2019-12-13 02:49:39
问题 Given this two objects (I use a very different objects to clarify better): public class Car { public string Brand {get;set;} public int Speed {get;set;} } public class Apple { public string Variety {get;set;} public int Production {get;set;} } AutoMapper defines the projection that allows mapping properties with different name: var config = new MapperConfiguration(cfg => { cfg.CreateMap<Car, Apple>() .ForMember(dest => dest.Variety, opt => opt.MapFrom( src => src.Brand)) .ForMember(dest =>

Automapper DynamicMap missing

时光毁灭记忆、已成空白 提交于 2019-12-13 02:49:00
问题 I was reading in a previous question on "How to use AutoMapper to map a DataRow to an object in a WCF service?" and I thought 'Great! That's what I'm looking for on mapping a table from MySQL!', yet after I got the Nuget package and tried to use that line of code like this: List<Customer> c = AutoMapper.Mapper.DynamicMap<IDataReader, List<Customer>>(dt.CreateDataReader()); I got this exception: 'Mapper' does not contain a definition for 'DynamicMap' After checking around on the 'net about it,