automapper

Problem auto mapping => collection of view models instead another view model

谁说胖子不能爱 提交于 2019-12-10 12:17:16
问题 I have something like this public class AViewModel { public decimal number { get; set; } public List<BViewModel> BVM { get; set; } } public class BViewModel { public string someString{ get; set; } } public class SomeObject { public decimal number { get; set; } public List<OtherObjects> BVM { get; set; } } public class OtherObjects { public string someString{ get; set; } } Mapper.CreateMap<SomeObject,AViewModel>(); When I have this I get Trying to map OtherObjects to BViewModel Using mapping

Make AutoMapper automatically map prefixed properties

你。 提交于 2019-12-10 11:47:20
问题 I want AutoMapper to map automatically Members like this: class Model { public int ModelId { get; set; } } class ModelDto { public int Id { get; set; } } Here, I would do a CreateMap<Model, ModelDTO>() .ForMember(x => x.Id, e => e.MapFrom(x => x.ModelId) But, how could I make AutoMapper do the mapping automatically? Most of my classes are like that. The Primary key is in the form: ClassName + "Id". EDIT: I've tried with this, but it doesn't work: class Program { static void Main(string[] args

AutoMapper and Entity Framework POCO with explicit loading

送分小仙女□ 提交于 2019-12-10 11:22:33
问题 I explicitly load relationships on my POCO when I need to, but since I switched to AutoMapper I thought I could instruct it to pre-load relationships for me so that my code in service layer looks cleaner: Mapper.CreateMap<Issue, IssueEditModel>().BeforeMap((i, m) => LoadProperties<Issue>(() => { return kernel.GetService<IIssuesRepository>(); }, i, new Expression<Func<Issue, object>>[] { e => e.RelationshipA, e => e.RelationshipB } ) ); The LoadProperties method looks up the repository using

Inferring destination type from interface with AutoMapper

杀马特。学长 韩版系。学妹 提交于 2019-12-10 10:55:31
问题 I'm trying to implement some cross-cutting concerns in my application, which uses AutoMapper to map between different DTOs/message objects. Say I have this: configuration.Map<MyMessage, MyEvent>() . MyEvent implements IEvent (which is a marker interface with no properties). Is there any way to ask AutoMapper to map a MyMessage to IEvent , and have it infer that "oh, I have a mapping MyMessage to MyEvent , and MyEvent implements IEvent "? This (invalid) example shows what I want to achieve: //

AutoMapper.AutoMapperMappingException

假装没事ソ 提交于 2019-12-10 10:18:51
问题 I know a lot of questions have been asked regarding this topic, but none of them provided an answer for my problem. That's why I'm creating a new question. I've looked on google and here for answers and have found some which have improved my setup of AutoMapper. (i.e. only creating your mappings only once) My problem is that I sometimes get the exception and sometimes I don't. And I have absolutely no idea how to solve it. I've been searching a whole day now and I can't find anything. When I

How to specify the shape of results with WebApi2, OData with $expand

半世苍凉 提交于 2019-12-10 09:07:29
问题 I have a problem executing my AutoMapper mappings when using OData with specific $select or $expand values. Using the WebApi Action: public IQueryable<BookInRequestDto> Get(ODataQueryOptions<BookInRequest> query) { var results = query.ApplyTo(_context.BookInRequests) as IQueryable<BookInRequest>; var mappedResults = Mapper.Map<IQueryable<BookInRequest>, IQueryable<BookInRequestDto>>(results); return mappedResults; } When I query: api/Get , I get an appropriate response, but a Document's

Automapper, mapping single destination property as a concatenation of multiple source property

六眼飞鱼酱① 提交于 2019-12-10 04:35:48
问题 I have a situation where I need to map a single property as a combination of multiple source properties based on some conditions. Destination : public class Email { public Email() { EmailRecipient = new List<EmailRecipient>(); } public string Subject{get; set;} public string Body {get; set;} public virtual ICollection<EmailRecipient> EmailRecipient { get; set; } } public class EmailRecipient { public int EmaiId { get; set; } public string RecipientEmailAddress { get; set; } public int

Initializing AutoMapper in an Azure Function

烈酒焚心 提交于 2019-12-10 03:52:26
问题 I am trying to create an Azure function in which I am using some code with AutoMapper. I am quite new to C#, Azure and AutoMapper and I'm having some trouble finding the correct way of initializing my AutoMapper configuration. MapInitializer.cs: public static class MapInitializer { public static void Activate() { Mapper.Initialize(cfg => { // initialize mappings here }); } } Then in my function, I am trying to do the following: Function.cs: public static class ProcessQueueForIntercom {

Am I using Automapper 2.0's Include functionality correctly?

陌路散爱 提交于 2019-12-10 03:15:09
问题 Either I'm not, or it isn't working... I have a single Source class that I want to map to multiple views that inherit from each other. Basically the base class is the Detail, and the child class is Edit or Update which use all the same data as Detail, plus a couple other fields to manage their own lists or whatever. Here are the maps I'm using: Mapper.CreateMap<Ticket, Detail>() .Include<Ticket, Update>() .Include<Ticket, Edit>() .ForMember(dest => dest.Priority, opt => opt.MapFrom(src => src

Automapper, Mapper Not initialized. Call initialize with proper configuration

爷,独闯天下 提交于 2019-12-10 01:49:57
问题 When I try to submit data into database i get following error: {"Success":false,"Error":true,"ErrorType":2,"Message":"System.InvalidOperationException: Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you\u0027re using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate