automapper

AutoMapper how to map a CamelCase Dictionary<string, object> to a PascalCase C# object

£可爱£侵袭症+ 提交于 2019-12-11 04:25:56
问题 I'm trying to map a Dictionary<string, object> where properties are keyed with Camel casing to a C# object with Pascal casing: var id = Guid.NewGuid(); var dto = new Dictionary<string, object> { { "id", id.ToString() }, { "value", "some value" }, }; var mapper = new MapperConfiguration(cfg => { }).CreateMapper(); var result = mapper.Map<TestClass>(dto); var expected = new TestClass { Id = id, Value = "some value", }; result.Should().BeEquivalentTo(expected); If I put the dictionary keys as

How C# Automapper can set the fields to null when the string is empty

本小妞迷上赌 提交于 2019-12-11 04:16:22
问题 Is there a way to set all properties of a class, that I'm mapping, that is string.Empty should map to NULL . Mapper.CreateMap<TSource, TDest>(); I want that all Properties of TSource that are string Empty are mapped to NULL in TDest corresponding Properties. I've currently not found a way to globally assign this condition without setting it up for all Properties manually. EDIT I need it only for a specific mapping not for all maps defined in my application. 回答1: cfg.CreateMap<string, string>(

Linq Projection is not including all data from the original entity

柔情痞子 提交于 2019-12-11 03:47:47
问题 I have encountered a LINQ issue and hope that you can help me to figure it out. Here is what is happening. I get an IQueryable<LicenseEntity> of entities from the repository. I look at the fields in these entities and see that they contain valid data. There is a field for a related entity Customer in the LicenseEntity. It contains valid data, too, because I loaded it with the LicenseEntity. I use .Select to project each LicenseEntity to a LicenseViewModel. For each LicenseEntity, a

Can I get AutoMapper to return the same object sometimes?

本小妞迷上赌 提交于 2019-12-11 03:33:02
问题 I've been using AutoMapper to map between an interface and a concrete implementation of that interface. I assumed that if the type that I passed in to AutoMapper's Map<TDestination> method was the same as the return type, then the original object would be returned (as a sort of short-circuiting operation). My assumption was wrong: indeed, after looking I noticed the documentation for that method explicitly states: Execute a mapping from the source object to a new destination object. The

Missing type map configuration or unsupported mapping after auto code generation on the client

∥☆過路亽.° 提交于 2019-12-11 03:32:17
问题 I have a simple class: [DataContract] public class ClaimView { [DataMember] public string Type { get; set; } [DataMember] public string Value { get; set; } [DataMember] public string ValueType { get; set; } } I receive this class on the client side by wcf server references and try to map it to System.Security.Claims.Claim like that: Mapper.CreateMap<ClaimView, Claim>() .ForMember(dest => dest.Type, opt => opt.MapFrom(src => src.Type)) .ForMember(dest => dest.Value, opt => opt.MapFrom(src=>

AutoMapper: Int to String and back again

非 Y 不嫁゛ 提交于 2019-12-11 03:22:22
问题 EDIT: to include TypeConverter To set the stage, I am stripping out code from a existing WCF service to put into a business object (BO) that will be referenced by the WCF to provide clients with information. A requirement is to make the employeeId of an Employee object to be an integer rather than the string currently used. I'm using AutoMapper to map all of the objects between the BO and WCF so that contracts do not break. However I'm struggling with how to provide the mapping back and forth

How do I conditionally set the destination object to null using automapper

匆匆过客 提交于 2019-12-11 03:15:23
问题 I am trying to do something like this: AutoMapper.Mapper.CreateMap<UrlPickerState, Link>() .ForMember(m=>m.OpenInNewWindow,map=>map.MapFrom(s=>s.NewWindow)) .AfterMap((picker, link) => link = !string.IsNullOrWhiteSpace(link.Url)?link:null) ; var pickerState = new UrlPickerState(); var linkOutput = AutoMapper.Mapper.Map<Link>(pickerState); However, the assigned value of link is not used in any execution path. I would like linkOutput to be null, but it is not. How would I make the destination

Issue with Ignoring nested Properties using Automapper

给你一囗甜甜゛ 提交于 2019-12-11 03:14:32
问题 I have ran into an issue where I am trying to ignore properties within properties. e.g. Mapper.CreateMap<Node, NodeDto>() .ForMember(dest => dest.ChildNodes, opt => opt.Ignore()) .ForMember(dest => dest.NodeType.EntityType.Properties, opt => opt.Ignore()); I get following exception: {"Expression 'dest => dest.NodeType.EntityType.Properties' must resolve to top-level member.\r\nParameter name: lambdaExpression"} Any idea? 回答1: Well I have managed to figure it out by myself. I have to specify

Merge two collections with Automapper by condition

二次信任 提交于 2019-12-11 02:57:06
问题 There are two types: 1) DTO type: [DataContract] public sealed class OrderDetailDto { [DataMember] public Guid MergeId { get; set; } [DataMember] public int Id { get; set; } [DataMember] public string PostionName { get; set; } [DataMember] public decimal Quantity { get; set; } [DataMember] public byte[] Version { get; set; } } 2) corresponding domain type: public sealed class OrderDetail { public Guid MergeId { get; set; } public int Id { get; set; } public string PostionName { get; set; }

What's the alternative to IValueFormatter in AutoMapper?

我只是一个虾纸丫 提交于 2019-12-11 02:54:01
问题 So I started using AutoMapper for my ASP.NET MVC project, looked at a couple of examples and wrote my first IValueFormatter implementation when a blue wiggly popped up for IValueFormatter: Interface 'AutoMapper.IValueFormatter' is obsolete: "Formatters should not not be used". Well, okay, then how am I supposed to e.g. convert a DateTime to, say, a string? 回答1: Just create mapping for the property without using IValueFormatter. For example: Mapper.CreateMap<DbItem, ItemView>() .ForMember(item