automapper

Automapper map from inner property to destination class

和自甴很熟 提交于 2019-12-04 05:05:41
Cant' seem to figure this one out. public class DestinationClass { public int InnerPropertyId { get; set; } public string StrignValue { get; set; } } public class SourceClass { public InnerValue Inner { get; set; } } public class InnerValue { public int InnerPropertyId { get; set; } public string StrignValue {get;set;} } I need to map from SourceClass.InnerValue directly to DestinationClass. How do I do that? Thanks in advance. As usual, right after I hit post question button: Mapper.Reset(); // from, to Mapper.CreateMap<InnerValue, DestinationClass>(); Mapper.CreateMap<SourceClass,

How to update an existing Entity from ViewModel using Automapper and EF4 DbContext with Lazy Loading enabled

六月ゝ 毕业季﹏ 提交于 2019-12-04 04:56:52
I am using Automapper to map between Entity and ViewModel object (in both directions). The model uses EF4 DbContext POCOs and needs LazyLoading (and therefore Proxy Generation) enabled. I have come across a problem attempting to update an existing entity from a viewmodel. When I call Mapper.Map(vm, entity), Automapper throws an exception. My question is: how are you supposed to work with EF Proxy objects using Automapper? The code looks (simplified) like this: public class MyEntity { public int Id {get;set;} public int Name {get;set;} } public class ViewModel { public int Id {get;set;} public

Using AutoMapper with Data Reader

萝らか妹 提交于 2019-12-04 04:49:00
I went through How can I easily convert DataReader to List<T>? I wanted to implement something like what is accepted as an answer in the above link. Scenrio: I am using OdbcDataReader to retrieve from the database. And I have a Model Class . FYI , the properties of this class are exact replica of the column names from the database. I need to map these columns to the properties and return List Can this be accomplished using Automapper. Viacheslav Avsenev Something like this public List<T> ReadData<T>(string queryString) { using (var connection = new SqlConnection(constr)) using (var command =

How to expose part of Entity as DataContract?

筅森魡賤 提交于 2019-12-04 04:12:42
问题 Up to now, when working with WCF, I have always been exposing the whole either EF generated entities, or POCOs(by modifying the T4 template to include DataContract and DataMember on POCOs and properties) as DataContract. Now, I have come across a situation that I cannot expose the whole thing, and need to explicitly specify my DataContract to be a subset of the entities. It worth saying that one of my entities is something like below: And I want to just expose Id, Name, CategoryId, Price.

AutoMapper How to map nested object from an ObjectId

牧云@^-^@ 提交于 2019-12-04 04:10:38
I am trying to map the ReferralContract.AssessmentId property to Referral.Assessment.Id The below code works but I am sure that there is a cleaner way to do.... Please tell me this is so ;-) // Destination classes public class Referral { public Referral() { Assessment = new Assessment(); } public int Id { get; set; } public Assessment Assessment { get; set; } } public class Assessment { public int Id { get; set; } } // Source Class public class ReferralContract { public int Id { get; set; } public int AssessmentId { get; set; } } The Automapper mapping I am using is Mapper.CreateMap

Should I use AutoMapper in my unit tests?

一世执手 提交于 2019-12-04 03:41:51
I'm writing unit tests for ASP.NET MVC controller methods. Those controllers have a dependency on IMapper - an interface I've created to abstract AutoMapper, passed in via constructor injection using Castle Windsor. Action methods use IMapper to map from domain objects to ViewModel objects and back again, aiming to keep things DRY and keep action methods succinct. In my unit tests, should I Configure AutoMapper with the correct bindings (they're built using AutoMapper profiles, so testable and reusable between website and unit test projects) and pass that in as a proper AutoMapper

automapper class and nested class map to one class

℡╲_俬逩灬. 提交于 2019-12-04 03:27:02
问题 i have written alot of description but i figured making a picture will make my problem clearer than words i have written this to map but it throws an exception Mapper.CreateMap<GenericStory, GenericStoryDisplayViewModel>().ForMember( gs => gs.StoryBody,dest => dest.MapFrom( gs => gs)); Trying to map StoryWriting.Web.Models.GenericStory to StoryWriting.Web.ViewModels.StoryBodyViewModel. Using mapping configuration for StoryWriting.Web.Models.GenericStory to StoryWriting.Web.ViewModels

The differences between a normal map with dynamic map - automapper

萝らか妹 提交于 2019-12-04 03:09:48
问题 What are the differences between the codes below; List<Ogrenci> ogrenci = AutoMapper.Mapper.DynamicMap<IDataReader, List<Ogrenci>>((dt.CreateDataReader())); var ogr = AutoMapper.Mapper.Map<IDataReader, IList<Ogrenci>>(dt.CreateDataReader()); When i try to use the code below; AutoMapper.Mapper.CreateMap<IDataReader, Ogrenci>().ForMember(dest => dest.Numarasi, opt => opt.MapFrom(src => Convert.ToInt32(src["Numara"]))) .ForMember(dest => dest.Adi, opt => opt.MapFrom(src => Convert.ToString(src[

Ignore a property in AutoMapper?

做~自己de王妃 提交于 2019-12-04 03:05:33
I'm using Automapper to copy one object properties to other and later will update in database using EF. Question is how to tell Automapper copy every property but ignore a particular property (in this case it will be Id). I'm new to AutoMapper and just have done this code. I don't have other configurations or use of AutoMap in project. Mapper.Map(lead, existingLead); I have downloaded AutoMapper form here https://github.com/AutoMapper/AutoMapper On your Mapper.CreateMap<Type1, Type2>() you can use either .ForSourceMember(x => x.Id, opt => opt.Ignore()) or .ForMember(x => x.Id, opt => opt

How to find the source property based on the name of a flattened property with AutoMapper

谁都会走 提交于 2019-12-04 02:50:41
I'm using AutoMapper and I'd like it to trace back a source property based on the name of the mapped (flattened) destination property. This is because my MVC controller has the name of a mapped property that it needs to provide to a service call that for sorting purposes. The service needs to know the name of the property that the mapping originated from (and the controller is not supposed to know it) in order to perform a proper call to the repository that actually sorts the data. For example: [Source.Address.ZipCode] maps to [Destination.AddressZipCode] Then Trace "AddressZipCode" back to