automapper

Using AutoMapper to map the property of an object to a string

狂风中的少年 提交于 2019-12-28 05:57:19
问题 I have the following model: public class Tag { public int Id { get; set; } public string Name { get; set; } } I want to be able to use AutoMapper to map the Name property of the Tag type to a string property in one of my viewmodels. I have created a custom resolver to try to handle this mapping, using the following code: public class TagToStringResolver : ValueResolver<Tag, string> { protected override string ResolveCore(Tag source) { return source.Name ?? string.Empty; } } I am mapping using

Using AutoMapper to map the property of an object to a string

浪尽此生 提交于 2019-12-28 05:57:00
问题 I have the following model: public class Tag { public int Id { get; set; } public string Name { get; set; } } I want to be able to use AutoMapper to map the Name property of the Tag type to a string property in one of my viewmodels. I have created a custom resolver to try to handle this mapping, using the following code: public class TagToStringResolver : ValueResolver<Tag, string> { protected override string ResolveCore(Tag source) { return source.Name ?? string.Empty; } } I am mapping using

Simple Automapper Example

元气小坏坏 提交于 2019-12-28 02:04:46
问题 I am having a hard time to understand how to map certain objects. Please answer some questions about this simple example. Example code class User { private int id; private string name; } class Group { private int id; private string name; private List<User> users; } [DataContract] public class UserDto { [DataMember] public int id { get; set; } [DataMember] public string name{ get; set; } } [DataContract] public class GroupDto { [DataMember] public int id { get; set; } [DataMember] public

Using AutoMapper to unflatten a DTO

大城市里の小女人 提交于 2019-12-27 17:33:49
问题 I've been trying to use AutoMapper to save some time going from my DTOs to my domain objects, but I'm having trouble configuring the map so that it works, and I'm beginning to wonder if AutoMapper might be the wrong tool for the job. Consider this example of domain objects (one entity and one value): public class Person { public string Name { get; set; } public StreetAddress Address { get; set; } } public class StreetAddress { public string Address { get; set; } public string City { get; set;

Generic class of one type to another type in auto mapper

不羁岁月 提交于 2019-12-25 18:57:15
问题 I have this Generic Pagination class: i want to map PagedList<Caste> to PagedList<CasteModel> public class PagedList<T> { public PagedList() { } public PagedList(IList<T> source, int pageNumber, int pageSize) { this.TotalItems = source.Count; this.PageNumber = pageNumber; this.PageSize = pageSize; this.Items = source; } public int TotalItems { get; set; } public int PageNumber { get; set; } public int PageSize { get; set; } public IEnumerable<T> Items { get; set; } public int TotalPages =>

AutoMapper

纵然是瞬间 提交于 2019-12-25 11:21:49
先来看一点实例,两个类之间的映射。 首先定义两个类Source与DTOSource: 1 public class Source 2 { 3 public int Id { get; set; } 4 public string Content { get; set; } 5 } 6 7 public class DTOSource 8 { 9 public int Id { get; set; } 10 public string Content { get; set; } 11 } Source与DTOSource字段完全相同,来看看它俩如何通过AutoMapper转换,代码很简单。 1 Mapper.Initialize(x=>{ 2 x.CreateMap<Source,DTOSource>(); 3 }); 4 5 Source s = new Source{Id=1,Content="123"}; 6 7 DTOSource dto = Mapper.Map<DTOSource>(s); 第一步建立Source到DTOSource之间的映射,初始化一个Source实例后,来看下执行结果: 执行完成后,可以看到dto中的数据与之前初始化的s的数据是一样的,就像是直接将s拷贝了一份给dto,在两个类字段名定全相同的情况下如此

C# Automapper 6.1.1 - Collection/List destination properties loses values Entity Framework 6

夙愿已清 提交于 2019-12-25 09:35:00
问题 I have a problem when mapping collections with Automapper 6 and I can't find a solution. In the updatedArticle object below I have the old Created and Updated values left after mapping since they do not exist on the view model. However the values for Created and Updated in Descriptions are lost. The values that do come in via the view model are all updated correctly. What am I doing wrong? Automapper also loses Entity Framework 6 mapping for Article since those values are lost as well.

Automaper for collections of Expression<Func>

非 Y 不嫁゛ 提交于 2019-12-25 09:34:37
问题 I am using Automapper in my application and I faced an issue while mapping Expression>. I have: var predicateDto = _mapper.Map<Expression<Func<InventoryApplicationDto, bool>>, Expression<Func<tblInventoryApplication, bool>>>(predicate); var applications = await _repository.SearchActivePagedAsync(predicateDto, page, pageSize); This code works fine when I have only one value in predicate but it returns exception when I have List<T> as a predicate. -- EDIT 1 Service method: public async Task

Automapper: How to map IList to EntityCollection

ぃ、小莉子 提交于 2019-12-25 07:58:32
问题 I am using Automapper v1.1, since I am using .NET 3.5 as well as using Entity Framework. I'm trying to map a ViewModel to Model but keep running into errors. I have the below sample classes: OrderViewModel public class OrderViewModel{ public string OrderNo { get; set; } public IList<OrderItem> DisplayItems { get; set; } public OrderViewModel(){ DisplayItems = new List<OrderItem>(); } } Order (note: this class is auto-generated by EF) public class Order{ public string OrderNo { get; set; }

Automapper: How to map IList to EntityCollection

吃可爱长大的小学妹 提交于 2019-12-25 07:56:45
问题 I am using Automapper v1.1, since I am using .NET 3.5 as well as using Entity Framework. I'm trying to map a ViewModel to Model but keep running into errors. I have the below sample classes: OrderViewModel public class OrderViewModel{ public string OrderNo { get; set; } public IList<OrderItem> DisplayItems { get; set; } public OrderViewModel(){ DisplayItems = new List<OrderItem>(); } } Order (note: this class is auto-generated by EF) public class Order{ public string OrderNo { get; set; }