automapper

Automapper Project using

╄→гoц情女王★ 提交于 2019-12-10 21:07:51
问题 I am currently using automapper in my backend to map objects to models. I recently decided to use the following code to handle all my timezone conversions: cfg.CreateMap<DateTime?, DateTime?>() .ProjectUsing(i => DbFunctions.AddHours(i, offset.Hours)); cfg.CreateMap<DateTime, DateTime>() .ProjectUsing(i => DbFunctions.AddHours(i, offset.Hours).Value); Object.ProjectTo<ObjectModel>().SingleOrDefault(); then it works fine and the object is mapped and timezone converted However when i am using

Using Automapper to Copy Properties from a Dynamic

谁都会走 提交于 2019-12-10 20:58:42
问题 I have a dynamic object (actually, a JObject, from JSON.NET) being built dynamically from JSON. I want to have its properties copied to an existing object. The properties from the dynamic object should exist in the target object's type, if not, it's ok to have an error. I am looking at Automapper, latest version, for this. I tried to create a map from JObject to the proper type, but I don't think it'll work because the properties in the JObject are stored in an internal dictionary. Is this

AutoMapper throws Missing Map when Map exists

↘锁芯ラ 提交于 2019-12-10 19:07:58
问题 Specifications: .NET 4.5.1 - MVC 5.2.2 - EF 6.0 - AutoMapper 3.2.1 I was first having Proxy object error but was able to solve it by doing the following: AutoMapper 3.1.1 and Entity Framework 6.1 Proxy objects Once I fixed that error, I instantly got the following error message: For some reason, it says the map from Pages to PagesViewModel doesn't exist, even though it does. Here is my code: In Global.asax.cs : protected void Application_Start() { ConfigureAutomapper.Configure(); .... In

AutoMapper and reflection

旧巷老猫 提交于 2019-12-10 18:54:21
问题 My shared hosting company doesn't allow Reflection. How can I use AutoMapper? Do I have to specify for each property a .ForMember? Mapper.CreateMap<Person, PersonData>() .ForMember(dest => dest.Name, o => o.MapFrom(src => src.Name)) .ForMember(dest => dest.Address, o => o.MapFrom(src => src.Address)); thanks, Filip 回答1: Automapper uses reflection.emit , are you sure you can use Automapper? [Edit] Dont know of any that uses without reflection, even the one I had created XmlDataMapper on

Mapping child classes with parent injected in the constructor using AutoMapper

一个人想着一个人 提交于 2019-12-10 18:47:49
问题 I have a following class structure: class SrcChild { public bool SomeProperty { get; set; } } class SrcParent { public IEnumerable<SrcChild> Children { get; set; } } so the SrcParent has a collection of SrcChild objects. Now I want to map an instance of SrcParent to DstParent. Here are the destination classes: class DstChild { public bool SomeProperty { get; set; } public DstChild(DstParent parent) { if (parent == null) throw new ArgumentNullException(); } } class DstParent { public

AutoMapper set destination to null on condition of source property

只愿长相守 提交于 2019-12-10 17:56:17
问题 I'm mapping between two objects and based on a condition of the source I would like the destination to be null. For example, here are the classes: public class Foo { public int Code { get; set; } public string Name { get; set; } } public class Bar { public string Name { get; set; } public string Type { get; set; } } And my map: Mapper.CreateMap<Foo, Bar>() .AfterMap((s, d) => { if (s.Code != 0) d = null; }); But it seems to ignore the AfterMap. Bar is initialised although with all default

Mapping from two items with automapper

最后都变了- 提交于 2019-12-10 16:48:04
问题 In my data layer I have a repository that can return a list of items like this: new List<Item> { new Item { Title = "Something", DetailId = 500 }, new Item { Title = "Whatever", DetailId = 501 }, } The repository has another method that can return details for these items when given the items detail ID: // repository.GetDetail(500) would return something like: new ItemDetail { Id = 500, Size = "Large" } Now, in my service layer I would like to map a the above list into something like this: new

Automapper - ReverseMap() does not perform mapping

送分小仙女□ 提交于 2019-12-10 15:35:25
问题 I have the following 2 classes: public class ReferenceEngine { public Guid ReferenceEngineId { get; set; } public string Description { get; set; } public int Horsepower { get; set; } } public class Engine { public Guid Id { get; set; } public string Description { get; set; } public int Power { get; set; } } I am using automapper to perform a mapping from ReferenceEngine to Engine and vice versa. Notice that the properties ReferenceEngineId / Id and Horsepower / Power does not have the same

How to register an AutoMapper profile with Unity

試著忘記壹切 提交于 2019-12-10 15:27:06
问题 I have the following AutoMapper profile: public class AutoMapperBootstrap : Profile { protected override void Configure() { CreateMap<Data.EntityFramework.RssFeed, IRssFeed>().ForMember(x => x.NewsArticles, opt => opt.MapFrom(y => y.RssFeedContent)); CreateMap<IRssFeedContent, Data.EntityFramework.RssFeedContent>().ForMember(x => x.Id, opt => opt.Ignore()); } } And I am initializing it like this: var config = new MapperConfiguration(cfg => { cfg.AddProfile(new AutoMapperBootstrap()); });

Automapper, INamingConvention Camelcase properties to uppercase with underscore

不问归期 提交于 2019-12-10 15:26:25
问题 I have two classes one generetad by Entity Framework, the other is the class I use everywhere. My Class : public class Person { public string FirstName { get; set; } public string LastName { get; set; } } EF class : public class PERSON { public string FIRST_NAME { get; set; } public string LAST_NAME { get; set; } } I found the solution when the source is PERSON to Person , but I don't find the solution for Person to PERSON (the properties are in uppercase and underscore separator). The