automapper

Using AutoMapper to Map a DataTable to an Object (DTO)

泪湿孤枕 提交于 2019-11-30 12:47:10
I am trying to map a DataTable to an object (DTO) using AutoMappers DynamicMap feature. DataTable dt; dt = new dalAllInvestors().InvestorNameSearch(investorNameSearch); // Look at DynamicMap - Urgent List<dtoAPISimpleInvestor> apiObject = AutoMapper.Mapper.DynamicMap<IDataReader, List<dtoAPISimpleInvestor>>( dt.CreateDataReader()); return apiObject; public class dtoAPISimpleInvestor { public int FirmID { get; set; } public string FirmName { get; set; } public string Type { get; set; } public string Location { get; set; } } dt returns 10 rows but when you look at the apiObject it returns no

Mapping collections using AutoMapper

偶尔善良 提交于 2019-11-30 11:50:12
问题 I'm trying to map an array into an ICollection of type <T>. Basically I want to be able to do: Mapper.CreateMap<X[], Y>(); Where Y is Collection<T> Any ideas? 回答1: You don't need to setup your mapping for collections, just the element types. So just: Mapper.CreateMap<X, Y>(); Mapper.Map<X[], Collection<Y>>(objectToMap); See here for more info: http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays&referringTitle=Home 回答2: Now it looks like you can use: Mapper.CreateMap<X,Y>(); var

AutoMapper How To Map Object A To Object B Differently Depending On Context

删除回忆录丶 提交于 2019-11-30 11:38:45
问题 Calling all AutoMapper gurus! I'd like to be able to map object A to object B differently depending on context at runtime. In particular, I'd like to ignore certain properties in one mapping case, and have all properties mapped in another case. What I'm experiencing is that Mapper.CreateMap can be called successfully in the different mapping cases however, once CreateMap is called, the map for a particular pair of types is set and is not subsequently changed by succeeding CreateMap calls

Automapper together with Dependency Injection

末鹿安然 提交于 2019-11-30 11:26:01
问题 I currently have the following mapping: Mapper.CreateMap<Journal, JournalDto>(); Now, Journal contains a member named RefTypeID , which corresponding value exists in another table in the database; to look up this value, I have a service which handles a simple int -> string request. The automapper configuration currently happens in a static class at the start of the program. Is it okay to move the mapping code into a class which gets injected into my DI container or is there a better way? 回答1:

AutoMapper Update Actions in ASP.NET MVC

那年仲夏 提交于 2019-11-30 10:30:36
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 just update what I've mapped then it'd get rid of valuable data in the database because of course in

How to handle view model with multiple aggregate roots?

耗尽温柔 提交于 2019-11-30 09:55:46
At the moment, i got quite badly fashioned view model. Classes looks like this=> public class AccountActionsForm { public Reader Reader { get; set; } //something... } Problem is that Reader type comes from domain model (violation of SRP). Basically, i'm looking for design tips (i.e. is it a good idea to split view model to inputs/outputs?) how to make my view model friction-less and developer friendly (i.e. - mapping should work automatically using controller base class)? I'm aware of AutoMapper framework and i'm likely going to use it. So, once more - what are common gotchas when trying to

How to ignore all properties that are marked as virtual

…衆ロ難τιáo~ 提交于 2019-11-30 09:20:07
I am using virtual keyword for some of my properties for EF lazy loading. I have a case in which all properties in my models that are marked as virtual should be ignored from AutoMapper when mapping source to destination. Is there an automatic way I can achieve this or should I ignore each member manually? inquisitive You can create a mapping extension and use it: namespace MywebProject.Extensions.Mapping { public static class IgnoreVirtualExtensions { public static IMappingExpression<TSource, TDestination> IgnoreAllVirtual<TSource, TDestination>( this IMappingExpression<TSource, TDestination>

AutoMapper — inheritance mapping not working, same source, multiple destinations

和自甴很熟 提交于 2019-11-30 08:43:22
Can I use inheritance mapping in AutoMapper (v2.2) for maps with the same Source type but different Destination types? I have this basic situation (the real classes have many more properties): public abstract class BaseViewModel { public int CommonProperty { get; set;} } public class ViewModelA : BaseViewModel { public int PropertyA { get; set; } } public class ViewModelB : BaseViewModel { public int PropertyB { get; set; } } ViewModelA and ViewModelB are different representations of the same Entity class: public class Entity { public int Property1 { get; set; } public int Property2 { get; set

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

女生的网名这么多〃 提交于 2019-11-30 08:32:27
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 obsolete: 'Dynamically creating maps will be removed in version 5.0. Use a MapperConfiguration instance and

Using DynamicMap() and ignore null source value

99封情书 提交于 2019-11-30 08:23:04
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? 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. Srinath Subramani I solved it with DataMember property in destination type [DataMember(EmitDefaultValue =