automapper

CsvHelper: Replace missing Csv field with another expression?

主宰稳场 提交于 2019-12-12 18:04:27
问题 When a field is missing in the CSV file, an exception is thrown. I'd rather map another value (such as empty string) when a field is missing. Map(dest => dest.PatientID).Name("Patient ID"); CsvHelper.CsvMissingFieldException: 'Fields 'Patient ID' do not exist in the CSV file.' If the configuration setting IgnoreReadingExceptions is used, no records are read into the results. var csv = new CsvReader(sr); csv.Configuration.RegisterClassMap<TMap>(); csv.Configuration.IgnoreReadingExceptions =

Automapper ignore readonly properties

被刻印的时光 ゝ 提交于 2019-12-12 16:14:13
问题 I'm trying to map from one object to another that has a public readonly Guid Id, which I want to ignore. I have tried like this: Mapper.CreateMap<SearchQuery, GetPersonsQuery>() .ForMember(dto => dto.Id, opt => opt.Ignore()); This seems to fail because Id is readonly: AutoMapperTests.IsValidConfiguration threw exception: System.ArgumentException: Expression must be writeable Is there any way around this? 回答1: I don't think ReadOnly fields are supported by AutoMapper. Only way I could get it

AutoMapper with Ninject confusion

百般思念 提交于 2019-12-12 15:19:21
问题 For starters I'm using this module: public class AutoMapperModule : NinjectModule { public override void Load() { Bind<ITypeMapFactory>().To<TypeMapFactory>(); foreach (var mapper in MapperRegistry.AllMappers()) { Bind<IObjectMapper>().ToConstant(mapper); } Bind<AutoMapper.ConfigurationStore>().ToSelf().InSingletonScope().WithConstructorArgument("mappers", ctx => ctx.Kernel.GetAll<IObjectMapper>()); Bind<IConfiguration>().ToMethod(ctx => ctx.Kernel.Get<AutoMapper.ConfigurationStore>()); Bind

Nested object members null after mapping with Automapper

孤者浪人 提交于 2019-12-12 15:14:12
问题 I have an object public class Tenant : EntityBase { public virtual string Name { get; set; } public virtual string Description { get; set; } public virtual string CreatorName { get; set; } public virtual TenantState State { get; set; } public virtual Address Address { get; set; } public virtual IList<TenantActivity> Activities { get; set; } public virtual IList<AppUser> Users { get; set; } public virtual IList<TenantConfig> TenantConfigs { get; set; } .... } A DTO like so: public class

How to ignore properties of a specific type when using Automapper?

Deadly 提交于 2019-12-12 15:10:18
问题 Let's suppose that I have two types: class Type1 { public int Prop1 { get; set; } public string Prop2 { get; set; } public string Prop3 { get; set; } } class Type2 { public int Prop1 { get; set; } public string Prop2 { get; set; } public TypeToIgnore Prop3 { get; set; } } I want to map between these two types, but ignoring all the properties that have a TypeToIgnore . This is because I am iterating through all of them using reflection and make some custom mappings on them. Within a class

Mapping a grouped collection using AutoMapper

不羁岁月 提交于 2019-12-12 11:05:13
问题 I have the following code which is not working: var groupedZones = this._zoneDataManager.GetZonesGroupedByCountry(); IEnumerable<IGrouping<String, ZoneDTO>> zonesToReturn = Mapper.Map<IEnumerable<IGrouping<String, Zone>>, IEnumerable<IGrouping<String, ZoneDTO>>>(groupedZones); I keep getting the following exception: The value \"System.Collections.Generic.List 1[SCGRE.Business.Model.ZoneDTO]\" is not of type \"System.Linq.IGrouping 2[System.String,SCGRE.Business.Model.ZoneDTO]\" and cannot be

AutoMapper strings to enum descriptions

て烟熏妆下的殇ゞ 提交于 2019-12-12 10:50:43
问题 Given the requirement: Take an object graph, set all enum type properties based on the processed value of a second string property. Convention dictates that the name of the source string property will be that of the enum property with a postfix of "Raw". By processed we mean we'll need to strip specified characters e.t.c. I've looked at custom formatters, value resolvers and type converters, none of which seems like a solution for this? We want to use AutoMapper as opposed to our own

Do we need both Automapper and Automapper.Net4 dlls to use Automapper?

為{幸葍}努か 提交于 2019-12-12 10:37:17
问题 Do we need Automapper and Automapper.Net4 dlls together to use the Automaper functionality in our code. I mean can't we just have the one dll of them both. Using Automapper for the first time. Need help. Thanks in advance 回答1: All you need to do is do "Install-Package AutoMapper" and you're set. Because AutoMapper supports all major .NET platforms, things that are specific to your platform are in a platform-specific assembly. This is a very common approach for building cross-platform

AutoMapper - Map using the same source and destination object types

ε祈祈猫儿з 提交于 2019-12-12 10:35:44
问题 I'm using Automapper to take two objects of the same type and map any new values that have changed. I tried using the code below, but it keeps throwing an error and I'm not even sure if this can even be achieved with Automapper. For example: Mapper.CreateMap<UserDetails, UserDetails>(); UserDetails userDetails = Mapper.Map<UserDetails, UserDetails>(userDetailsCurrent, userDetailsNew); Basically, I need to copy across any new values that come in from the new object "userDetailsNew" to the

Automapper ResolveUsing cause “Can't resolve this to Queryable Expression”

ε祈祈猫儿з 提交于 2019-12-12 09:33:31
问题 I'm using autommaper to map domain classes to model classes and viceversa. I need to encrypt/decrypt one property. When I map Model to Domain there isn't problem, work perefectly: Mapper.CreateMap<EntityModel, Entity>().ForMember(dest => dest.Password, opt => opt.ResolveUsing(src => this.EncryptString(src.Password))) But when map Entity to Model automapper crash and throws "Can't resolve this to Queryable Expression": Mapper.CreateMap<Entity, EntityModel>().ForMember(dest => dest.Password,