automapper-4

How to stop Automapper from mapping to parent class when child class was requested

烂漫一生 提交于 2019-12-23 08:53:40
问题 I am working on implementing AutoMapper in our service and am seeing a very confusing issue in our unit tests. First off this issue involves the following objects and their respective maps: public class DbAccount : ActiveRecordBase<DbAccount> { // this is the ORM entity } public class Account { // this is the primary full valued Dto } public class LazyAccount : Account { // this class as it is named doesn't load the majority of the properties of account } Mapper.CreateMap<DbAccount,Account>()

How to stop Automapper from mapping to parent class when child class was requested

这一生的挚爱 提交于 2019-12-23 08:52:32
问题 I am working on implementing AutoMapper in our service and am seeing a very confusing issue in our unit tests. First off this issue involves the following objects and their respective maps: public class DbAccount : ActiveRecordBase<DbAccount> { // this is the ORM entity } public class Account { // this is the primary full valued Dto } public class LazyAccount : Account { // this class as it is named doesn't load the majority of the properties of account } Mapper.CreateMap<DbAccount,Account>()

Non-static AutoMapper and ASP.NET MVC

蓝咒 提交于 2019-12-13 01:45:09
问题 Similarly to this question: Where to place AutoMapper.CreateMaps? Where is the recommended place to put the non-static AutoMapper initialization? var map = new MapperConfiguration( cfg => ... ).CreateMapper(); Where is the recommended place to store the map variable in order for it to be accessible from controllers? Thanks in advance. 回答1: A good approach to this is to use dependency injection and inject the mapper on your components that need access to it. This new approach to AutoMapper is

Automapper map child list with from same type

谁都会走 提交于 2019-12-11 04:58:30
问题 I have entities that may have children and their children may have children and so on... When I get database models all entities are OK with correct children and parent. But the problem comes when I want to map to view model: Is there any possible way to map models from database like this? // database model code first public class Tomato { public int Id { get; set; } public string Name { get; set; } public int? ParentId { get; set; } public virtual Tomato Parent { get; set; } public

Cannot resolve AutoMapper.IMapper using AutoMapper 4.2 with Autofac

感情迁移 提交于 2019-12-07 08:07:35
问题 I have tried various permutations of this but my current configuration (as it relates to AutoMapper) is like this: builder.RegisterAssemblyTypes().AssignableTo(typeof(Profile)).As<Profile>(); builder.Register(c => new MapperConfiguration(cfg => { foreach (var profile in c.Resolve<IEnumerable<Profile>>()) { cfg.AddProfile(profile); } })).AsSelf().SingleInstance(); builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope(); builder

How to Initialize AutoMapper Profiles in referenced project DLLs in ASP.Net webapp

爷,独闯天下 提交于 2019-12-06 05:16:44
问题 Struggling a little on how to use automapper in my project class libraries (dlls). See my structure of my overall solution below. The WebApp fires up, and in Global.asax App Start, the AutoMapper.Configure() method is called to add the mapping profiles. For now I am just adding the Services.AutoMapperViewModelProfile. But I need to somehow account for the profiles in each of the WebStoreAdapters (BigCommerce and Shopify in the example below). I was hoping not to add references to each

Cannot resolve AutoMapper.IMapper using AutoMapper 4.2 with Autofac

北城以北 提交于 2019-12-05 13:38:39
I have tried various permutations of this but my current configuration (as it relates to AutoMapper) is like this: builder.RegisterAssemblyTypes().AssignableTo(typeof(Profile)).As<Profile>(); builder.Register(c => new MapperConfiguration(cfg => { foreach (var profile in c.Resolve<IEnumerable<Profile>>()) { cfg.AddProfile(profile); } })).AsSelf().SingleInstance(); builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope(); builder.RegisterType<MappingEngine>().As<IMappingEngine>(); I have a constructor using IMapper mapper , however

How to Initialize AutoMapper Profiles in referenced project DLLs in ASP.Net webapp

限于喜欢 提交于 2019-12-04 09:02:11
Struggling a little on how to use automapper in my project class libraries (dlls). See my structure of my overall solution below. The WebApp fires up, and in Global.asax App Start, the AutoMapper.Configure() method is called to add the mapping profiles. For now I am just adding the Services.AutoMapperViewModelProfile. But I need to somehow account for the profiles in each of the WebStoreAdapters (BigCommerce and Shopify in the example below). I was hoping not to add references to each WebStoreAdapter in WebApp, just for the sake of being able to add the profiles during the AutoMapperConfig. If

Automapper - Inheritance mapper not working with type converter

我与影子孤独终老i 提交于 2019-12-04 05:50:12
问题 Can't use Mapping Inheritance and TypeConverter together. I have this code: /* BaseClassTypeConverter.cs */ public class BaseClassTypeConverter : ITypeConverter<SourceClass, BaseClass> { public BaseClass Convert(ResolutionContext context) { if (context == null || context.IsSourceValueNull) return null; var src = (SourceClass)context.SourceValue; return new BaseClass() { CommonAttr = src.SourceAttr }; } } /* AutoMapperConfig.cs */ public static class AutoMapperConfig { public static void

Automapper - Inheritance mapper not working with type converter

感情迁移 提交于 2019-12-02 08:50:46
Can't use Mapping Inheritance and TypeConverter together. I have this code: /* BaseClassTypeConverter.cs */ public class BaseClassTypeConverter : ITypeConverter<SourceClass, BaseClass> { public BaseClass Convert(ResolutionContext context) { if (context == null || context.IsSourceValueNull) return null; var src = (SourceClass)context.SourceValue; return new BaseClass() { CommonAttr = src.SourceAttr }; } } /* AutoMapperConfig.cs */ public static class AutoMapperConfig { public static void RegisterMappings() { AutoMapper.Mapper.Initialize(config => { config .CreateMap<SourceClass, BaseClass>()