automapper

passing around values to an AutoMapper Type Converter from outside

风流意气都作罢 提交于 2019-12-03 00:36:19
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 { English, French, Italian, Maltese } public class MultilingualValue<T> { public Dictionary<Language, T> Value

How do I get AutoMapper to not cache mapped objects?

▼魔方 西西 提交于 2019-12-02 23:50:22
When AutoMapper encounters an object that's already been mapped, it seems to use that object again, instead of trying to re-map it. I believe it does this based on .Equals() . I have a tree that's being mapped. So, a node with some properties, and children. More than one of the nodes have the same value of .Equals() , because it's based off an Id property. The children of the nodes are different and I need those re-mapped, but it's using a cached map value. Is there a way to turn the cached mapping off? All I can think of is implementing a new converter, but that totally defeats the purpose of

.NET - Convert/map list into object and vis versa

北慕城南 提交于 2019-12-02 22:51:08
问题 I have the following list which contains field names and values. public class FormField { public string FieldName { get; set;} public string FieldValue { get; set;} } var formData = new List<FormField>(); formData.Add(new FormField { FieldName = "Date", FieldValue = "2017-09-14" }); formData.Add(new FormField { FieldName = "Name", FieldValue = "Job blogs" }); formData.Add(new FormField { FieldName = "IsEnabled", FieldValue = "true" }); How can I convert or map the list into the following

Mapping one source class to multiple derived classes with automapper

[亡魂溺海] 提交于 2019-12-02 22:21:18
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 to repeat it, is there any way to achieve this? Mapper.CreateMap<Source, DestinationBase>() .ForMember(.

C# AutoMapper 了解一下

匿名 (未验证) 提交于 2019-12-02 22:10:10
什么是AutoMapper? 简单来说就是将一个对象映射到另一个对象的代码。 摆脱了繁琐的赋值过程 (最常见也就是Model -――ViewModel) AutoMapper安装 也可以使用控制台命令 PM> Install-Package AutoMapper public class ShopingInfo:EntityBase { public string ShopingName { get; set; } public int ShopingCount { get; set; } public decimal ShopingPric { get; set; } public int Stock { get; set; } public int Volumeofvolume { get; set; } public int ShopingTypeId { get; set; } public virtual ShopingType ShopingType { get; set; } } public class ShopingInfoViewModel { public int ID { get; set; } public string ShopingName { get; set; } public int ShopingCount { get; set; }

简单的C#实体映射 AutoMapper

匿名 (未验证) 提交于 2019-12-02 22:06:11
AutoMapper 是对象到对象的映射工具。在完成映射规则之后, AutoMapper 可以将源对象转换为目标对象。 要映射实体 1 public class SourceModel 2 { 3 public int ID { get; set; } 4 public string Name { get; set; } 5 public string Address { get; set; } 6 public string Mobile { get; set; } 7 } View Code 被映射实体 1 public class YingSheModel 2 { 3 public string Name { get; set; } 4 public string Address { get; set; } 5 } View Code 需要将SourceModel类的对象映射到YingSheModel类的对象上面。需要对AutoMapper进行如下配置: //注:Mapper.CreateMap由于nuget的最新版本用法改变了无法使用 Mapper.Initialize(cret => cret.CreateMap<SourceModel, YingSheModel>()) 效果展示: 全部代码: } public class SourceModel { public

AutoMapper Map If Not Null, Otherwise Custom Convert

我与影子孤独终老i 提交于 2019-12-02 21:57:36
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 to do this? Worst case i could remove that mapping for that property, and just do: var mapped = Mapper

Mapping the 'count' on child list property in AutoMapper

时间秒杀一切 提交于 2019-12-02 21:49:52
问题 I am working in an ASP.NET MVC 4 application, and have something similar to this for my domain. public class Party { public Cake PartyCake { get; set; } public List<Candles> CakeCandles { get; set; } } I want to map that to the PartyVM which looks like so: public class PartyVM { public string PartyCakeName { get; set; } public int CandlesCount { get; set; } } How can I tell AutoMapper to work out the Count of the list when doing its mappings? I have this, but all this does is tell it to

Mapping two classes in c#

ⅰ亾dé卋堺 提交于 2019-12-02 21:07:38
问题 I have two classes public class foo1 { public int id; public string image_link; public string sale_price; } and public class foo2 { public int Id; public string ImageLink; public string SalePrice } The property values differ only by underscore and cases. I need to map these two classes. For now I am trying something like this and its working: //var b = object of foo2 var a = new foo1{ a.id = b.Id, a.image_link = b.ImageLink, a.sale_price = b.SalePrice } I heard of AutoMapper, but I din't have

AutoMapper Custom Type Converter not working

我是研究僧i 提交于 2019-12-02 21:07:21
I am using Troy Goode's PagedList to provide paging information in my WebApi. His package returns an IPagedList that implements IEnumerable but also contains custom properties such as IsLastPage, PageNumber, PageCount, etc. When you try to return this class from a WebApi controller method (such as the GET), the Enumerable is serialized, but the custom properties are not. So, I thought I would use AutoMapper and write a custom type converter to convert to a class such as this: public class PagedViewModel<T> { public int FirstItemOnPage { get; set; } public bool HasNextPage { get; set; } public