automapper

Using string.Split() in AutoMapper issue

一曲冷凌霜 提交于 2021-02-20 19:12:51
问题 I have an ASP .Net core application. I am simply trying to have my AutoMapper configure to convert a string comma delimited into a list of strings as per this configuration: configuration.CreateMap<Job, JobDto>() .ForMember(dto => dto.Keywords, options => options.MapFrom(entity => entity.Keywords.Split(',').ToList())) For some reason it does not get compiled and give me the following error: An expression tree may not contain a call or invocation that uses optional argument I can't see why I

Automapper and inheritance from Collection or List

只谈情不闲聊 提交于 2021-02-19 07:45:07
问题 I'm trying to use AutoMapper (v5.1.1) to map an object which inherits from a List or Collection. The map call does not give me an error but the output is an empty list (of correct type though). I can get a List<DestinationObject> or Collection<DestinationObject> , but it does not seem to work when having a custom class which enherits from List<T> or Collection<T> . I've tried extending the first map definition to include the base class ( List<T> ) but that gives me a StackOverflowException.

A kind of integration testing in ASP.NET Core, with EF and AutoMapper

对着背影说爱祢 提交于 2021-02-11 14:32:29
问题 I'm trying to test the path from my ASP.NET Core Controller to the DB, through repository which includes the usage of AutoMapper. Here's my repository: using System; using System.Linq; using AutoMapper; using DS.DTO.MasterData; using DS.Utilities.DSExceptions; using Microsoft.Extensions.Logging; using Omu.ValueInjecter; namespace DS.MasterData.Repositories { public class PersonFactRepository : IPersonFactRepository { private readonly Database.MasterDataContext dbContext; private readonly

Automapper: UseDestinationValue does not work for collections?

不羁的心 提交于 2021-02-11 13:40:57
问题 Let's assume there are classes: public class Foo { public List<Bar> Bars { get; set; } } public class Bar { public string Name { get; set; } public string Description { get; set; } } public class FooDto { public List<BarDto> Bars { get; set; } } public class BarDto { public string Name { get; set; } } public class MapperProfile : Profile { public MapperProfile() { CreateMap<FooDto, Foo>(); CreateMap<BarDto, Bar>() .ForMember(dest => dest.Description, opt => opt.UseDestinationValue()); } } If

Error “{”stateMachine“:{”<>1__state“:-2,”<>t__builder“:{” when run project netcore

给你一囗甜甜゛ 提交于 2021-02-08 13:48:12
问题 When I run project netcore I get a message {"stateMachine":{"<>1__state":-1,"<>t__builder":{ and I don't know how to fix this. I see error in command line Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] An unhandled exception has occurred while executing the request. Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'task' with type 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder` and Microsoft.AspNetCore.Server.Kestrel[13]

Expression表达式目录树

…衆ロ難τιáo~ 提交于 2021-02-08 08:59:43
一、初识Expression 源码 1、在上一篇我们讲到了 委托 (忘记了可以在看看,点赞在看养成习惯),今天要讲的Expression也和委托有一点点关系吧(没有直接关系,只是想要大家看看我其他的文章),Expression是.NET准备为Linq to Sql准备的,它的命名空间是System.Linq.Expressions 2、不知道大家有没有用户ORM(对象映射实体)的数据访问层框架,使用过的小伙伴我相信对下面的伪代码不会陌生,我们在Where中传入的就是Expression<Func<TSource, bool>> predicate 3、我们进入Expression一看究竟,我们可以看到Expression<Func<TSource, bool>>里面有一些方法(后面会慢慢道来),最终继承LambdaExpression 4、我们继续进入LambdaExpression,我们看到了一些属性(这些就是我们lambda的组成的方法和属性),但是最终还是看到继承了Expression   5、继续一鼓作气进入Expression,到这里我们看到了最终的基类它里面也有很多方法,要说的话这两天都说不完,我们就简单的介绍一些常用的 二、循序渐进 1、大家可能看了上面还有一点点蒙,不急我们继续,我们看下面的实际操作,我们可以看到我们创建一个Expression和一个委托

How to configure AutoMapper to globally Ignore all Properties With Inaccessible Setter(private or protected)?

余生长醉 提交于 2021-02-08 02:57:49
问题 How to Ignore mapping Package automatically without using IgnoreAllPropertiesWithAnInaccessibleSetter() ? cfg.CreateMap<Dto, InternetContract>(); public class InternetContract { public virtual string Package { get;protected set; } } public class Dto { public string Package { get; set; } } 回答1: Technically, this would do what you ask: Mapper.Initialize(cfg => { cfg.ShouldMapProperty = p => { var setMethod = p.GetSetMethod(true); return !(setMethod == null || setMethod.IsPrivate || setMethod

Automapper UseDestinationValue

喜欢而已 提交于 2021-02-07 18:19:20
问题 Having a problem with a mapping VPerson vPerson = new VPerson() { Id = 2, Lastname = "Hansen1", Name = "Morten1" }; DPerson dPerson = new DPerson() { Id = 1, Lastname = "Hansen", Name = "Morten" }; Mapper.Initialize(x => { //x.AllowNullDestinationValues = true; // does exactly what it says (false by default) }); Mapper.CreateMap(); Mapper.CreateMap() .ForMember(dest => dest.Id, opt => opt.UseDestinationValue()); Mapper.AssertConfigurationIsValid(); dPerson = Mapper.Map<VPerson, DPerson>

AutoMapper mapping to a property of a nullable property

主宰稳场 提交于 2021-02-07 11:45:29
问题 How can you map a property to a sub-property that may be null? eg the following code will fail with a NullReferenceException because the Contact's User property is null. using AutoMapper; namespace AutoMapperTests { class Program { static void Main( string[] args ) { Mapper.CreateMap<Contact, ContactModel>() .ForMember( x => x.UserName, opt => opt.MapFrom( y => y.User.UserName ) ); Mapper.AssertConfigurationIsValid(); var c = new Contact(); var co = new ContactModel(); Mapper.Map( c, co ); }

AddAutoMapper does not load all assemblies in asp.net core

别来无恙 提交于 2021-01-29 18:53:05
问题 I have the following code that add automapper to my app. public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddHttpClient(); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); } The AppDomain.CurrentDomain.GetAssemblies() returns only assemblies that were loaded at the time it is called. I can see that some of my assemblies which contain my mappings have not been yet loaded and as a