automapper

AutoMapper Update Actions in ASP.NET MVC

亡梦爱人 提交于 2019-12-18 14:01:49
问题 This is probably quite straight forward for some, however I'm a bit confused and can't find a decent example. Say I'm using view models and my POST action takes in that view model. Typically I would do something along the following lines: [HttpPost] public ActionResult Update(UserViewModel uvm) { User user = Mapper.Map<UserViewModel, User>(uvm); _repository.Update(user); return RedirectToAction("Index"); } Although this isn't the full picture. The mapping would work fine, however if I were to

AutoMapper.Mapper.CreateMap<TSource,TDestination>()' is obsolete

a 夏天 提交于 2019-12-18 13:04:29
问题 I have to classes Like class A { public int id {get; set;} } class B { public C c {get; set;} } class C { public int id {get; set;} public string Name {get; set;} } My requirement is to map id of class A to id of class C. Now what I was doing till now was: Mapper.CreateMap().ForMember(des => des.C.Id, src => src.MapFrom(x => x.id)); and it was working fine. Now seems like Auto mapper has changed their implementation. and I am getting warning as below: AutoMapper.Mapper.CreateMap()' is

Using DynamicMap() and ignore null source value

时间秒杀一切 提交于 2019-12-18 12:55:09
问题 I'm using Mapper.DynamicMap() inside a generic method and would like to, without using .CreateMap() , ignore some any source values that are null. Is this even possible? 回答1: If you want all source properties with null values to be ignored you could use: Mapper.CreateMap<SourceType, DestinationType>() .ForAllMembers(opt => opt.Condition(srs => !srs.IsSourceValueNull)); Otherwise, you can do something similar for each member. This will get quit tedious if there are a large number of properties

A better way to use AutoMapper to flatten nested objects?

a 夏天 提交于 2019-12-18 11:44:51
问题 I have been flattening domain objects into DTOs as shown in the example below: public class Root { public string AParentProperty { get; set; } public Nested TheNestedClass { get; set; } } public class Nested { public string ANestedProperty { get; set; } } public class Flattened { public string AParentProperty { get; set; } public string ANestedProperty { get; set; } } // I put the equivalent of the following in a profile, configured at application start // as suggested by others: Mapper

AutoMapper Mapping IEnumerable to DataReader Issue

泪湿孤枕 提交于 2019-12-18 11:39:53
问题 I am using AutoMapper to datareader using code as discussed below http://elegantcode.com/2009/10/16/mapping-from-idatareaderidatarecord-with-automapper/ I see it being very flakky...and unpredictable. 1) Same code with same datareader at times brings value back to the dto result set and at times doesnot. 2) I have an ID value coming from database as 100, 200. When it maps to the DTO that is of type integer this 100 is changed to a large value (like 234343211). Any ideas on why am I seeing

AutoMapper Custom Type Converter not working

主宰稳场 提交于 2019-12-18 11:33:20
问题 I am using Troy Goode's PagedList to provide paging information in my WebApi. His package returns an IPagedList that implements IEnumerable but also contains custom properties such as IsLastPage, PageNumber, PageCount, etc. When you try to return this class from a WebApi controller method (such as the GET), the Enumerable is serialized, but the custom properties are not. So, I thought I would use AutoMapper and write a custom type converter to convert to a class such as this: public class

AutoMapper recursive

让人想犯罪 __ 提交于 2019-12-18 07:10:43
问题 I would like to make a deep copy of a complex object tree using AutoMapper. The problem is that for each member I would like to construct a new object and then map it, and not simply copying the existing one. Here it is an example: public abstract class Test { public Test() { this.Id = Guid.NewGuid(); } public Guid Id { get; private set; } } public class OuterTest : Test { public InnerTest Inner { get; set; } } public class InnerTest : Test { public int Value { get; set; } } and how to test

Automapper - Mapper already initialized error

独自空忆成欢 提交于 2019-12-18 05:27:19
问题 I am using AutoMapper 6.2.0 in my ASP.NET MVC 5 application. When I call my view through controller it shows all things right. But, when I refresh that view, Visual Studio shows an error: System.InvalidOperationException: 'Mapper already initialized. You must call Initialize once per application domain/process.' I am using AutoMapper only in one controller. Not made any configuration in any place yet nor used AutoMapper in any other service or controller. My controller: public class

Custom Mapping with AutoMapper

妖精的绣舞 提交于 2019-12-18 04:36:07
问题 I have two very simple objects: public class CategoryDto { public string Id { get; set; } public string MyValueProperty { get; set; } } public class Category { public string Id { get; set; } [MapTo("MyValueProperty")] public string Key { get; set; } } When mapping a Category to a CategoryDto with AutoMapper, I would like the following behavior: The properties should be mapped as usual, except for those that have the MapTo attribute. In this case, I have to read the value of the Attribute to

Automapper to create object from XML

假如想象 提交于 2019-12-18 04:18:13
问题 If I have the following class: class SPUser { public int ID { get; set; } public string Name { get; set; } public string LoginName { get; set; } public string Email { get; set; } public bool IsSiteAdmin { get; set; } public bool IsSiteAuditor { get; set; } public bool IsDomainGroup { get; set; } public List<SPGroup> Groups { get; set; } } And I am using the sharepoint web services, which return an XML with an attribute for each property on my class, such as: <Users> <User Name="name"