automapper

Automapper fails when projecting IQueryable<object> when object has a collection property

为君一笑 提交于 2019-12-03 09:06:48
So, here's my situation. I've got a my two classes: class FromClass { public string[] Foo { get; set; } } class ToClass { public string[] Foo { get; set; } } The classes have properties which are arrays. They could be List<T> or IEnumerable<T> , I get the same result in any of those cases. I try to map from one to the other using the AutoMapper.QueryableExtensions : class Program { static void Main(string[] args) { // create a "From" object string[] anArray = new string[] { "a", "b" }; FromClass anObject = new FromClass() { Foo = anArray }; // make a queryable set that includes the "From"

passing around values to an AutoMapper Type Converter from outside

核能气质少年 提交于 2019-12-03 08:58:51
问题 I have a multilingual database, which returns values based on a key and an enum Language . When I convert a DB object to a model, I want the model to contain the translated value based on the key and the current language. The key comes from the DB object but how can I pass the current language to the the Mapper.Map() function? Currently, I am using a [ThreadStatic] attribute to set the culture before calling Mapper.Map<> , and to retrieve it in the TypeConverter . public enum Language {

Automapper enum to Enumeration Class

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to use Automapper to map from a regular enum to an Enumeration Class (as described by Jimmy Bogard - http://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/ ). The regular enum doesn't have the same values as the enumeration class does. I would therefore like to map using the Name if possible: Enum: public enum ProductType { ProductType1, ProductType2 } Enumeration Class: public class ProductType : Enumeration { public static ProductType ProductType1 = new ProductType(8, "Product Type 1"); public static ProductType

Entity Framework, AutoMapper, handling entity updates

时光总嘲笑我的痴心妄想 提交于 2019-12-03 08:37:46
I just started using the Entity Framework 1.0 recently and believe I am beginning to feel the pains everyone is talking about. I'm trying to use best practices so I have a set of DTO that get mapped to and from my Entities via AutoMapper. The real catch is when I'm trying to update an object. The first gotcha was that I could not find a way to create a new entity, transfer the data from my DTO, and still have the entity ObjectContext realize that it has been changed. I used the following code: public VideoDTO UpdateVideo(VideoDTO pVideo) { Video video = new Video(); Mapper.Map(pVideo, video);

Automapper: Map to Protected Property

匿名 (未验证) 提交于 2019-12-03 08:36:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I need to map to a protected property on a class using Automapper . I've got a public method exposed on this class that is used to set values to the property. This method requires a parameter . How can I map a value to this class? Destination Class: public class Policy { private Billing _billing; protected Billing Billing { get { return _billing; } set { _billing = value; } } public void SetBilling(Billing billing) { if (billing != null) { Billing = billing; } else { throw new NullReferenceException("Billing can't be null"); } } } Here's

Automapper - Ignore mapping with condition

匿名 (未验证) 提交于 2019-12-03 08:36:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm using automapper and I would like to know if it's possible to ignore a mapping of a field when that's null. That's my code: .ForMember(dest => dest.BusinessGroup_Id, opt => opt.MapFrom(src => (int)src.BusinessGroup)) src.BusinessGroup type = "enum" dest.BusinessGroup_Id = int The objective it's to ingore that Mapping if src.BusinessGroup = null. 回答1: I think NullSubstitute option will do the trick .ForMember(d => d.BusinessGroup_Id, o => o.MapFrom(s => (int?)s.BusinessGroup)); .ForMember(d => d.BusinessGroup_Id, o => o.NullSubstitute(0))

Mapping one source class to multiple derived classes with automapper

你说的曾经没有我的故事 提交于 2019-12-03 08:35:27
问题 Suppose i have a source class: public class Source { //Several properties that can be mapped to DerivedBase and its subclasses } And some destination classes: public class DestinationBase { //Several properties } public class DestinationDerived1 : DestinationBase { //Several properties } public class DestinationDerived2 : DestinationBase { //Several properties } Then I wish the derived destination classes to inherit the automapper configuration of the baseclass because I do not want to have

IQueryable Extension Behavior Differing For Automapper Polymorphic Collection

匿名 (未验证) 提交于 2019-12-03 08:33:39
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Using Automapper 3.3.1.0 there is a different mapping behavior between the usage of Mapper.Map<IEnumerable<TDestination>>(someEnumerable) compared to someEnumerable.AsQueryable().Project().To<TDestination>() This does not appear to be a limitation of a SQL LINQ provider or other as this is witnessed in an in-memory collection. As with many things this is best explained by example: Note: the following code can be found at https://gist.github.com/kmoormann/b3949d006f4083ab6ee4 using System . Collections . Generic ; using System .

Reusing validation attributes in custom ViewModels

妖精的绣舞 提交于 2019-12-03 08:27:41
问题 When I started using xVal for client-side validation, I was only implementing action methods which used domain model objects as a viewmodel or embedded instances of those objects in the viewmodel. This approach works fine most of the time, but there are cases when the view needs to display and post back only a subset of the model's properties (for example when the user wants to update his password, but not the rest of his profile data). One (ugly) workaround is to have a hidden input field on

AutoMapper Map If Not Null, Otherwise Custom Convert

*爱你&永不变心* 提交于 2019-12-03 08:16:37
问题 Here's my code: Mapper.CreateMap<Foo, Foo2>() .ForMember(dest => dest.Bar, opt => opt.MapFrom(src => src.Bar == null ? new BarViewModel() : src.Bar)) Basically, "BarViewModel" has a parameterless ctor which sets up properties in the class. So i'm trying to say to AutoMapper: If the value is null, then use the ctor for the class. otherwise use the mapping you have in place The above is giving me a C# compiler error. And i'm guessing a cast wouldn't work either. So is there a AutoMapper trick