automapper

passing around values to an AutoMapper Type Converter from outside

匿名 (未验证) 提交于 2019-12-03 02:49:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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

Entity Framework + AutoMapper ( Entity to DTO and DTO to Entity )

匿名 (未验证) 提交于 2019-12-03 02:44:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've got some problems using EF with AutoMapper. =/ for example : I've got 2 related entities ( Customers and Orders ) and they're DTO classes : class CustomerDTO { public string CustomerID {get;set;} public string CustomerName {get;set;} public IList< OrderDTO > Orders {get;set;} } class OrderDTO { public string OrderID {get;set;} public string OrderDetails {get;set;} public CustomerDTO Customers {get;set;} } //when mapping Entity to DTO the code works Customers cust = getCustomer(id); Mapper.CreateMap< Customers, CustomerDTO >(); Mapper

在ASP.NET Core中使用AutoMapper

你。 提交于 2019-12-03 02:39:19
普通的模型映射 现在有两个类,实体类Student和返回展示的 View类StudentView 两个实体类字段还基本可以,不是很多,假如字段非常非常多,还需要手动去赋值的话,简直太痛苦了。 想了想这才是一个方法,一般的系统都会少则几十,多则上百个这样的方法,这还不算,大家肯定遇到过一个情况,如果有一天要在页面多显示一个字段,噗!不是吧,首先要存在数据库,然后在该实体类就应该多一个,然后再在每一个赋值的地方增加一个,而且也没有更好的办法不是,一不小心就少了一个,然后被产品测试说咱们不细心,心塞哟,别慌!神器来了,一招搞定。 先来引入DTO讲解 我们已经知道了ORM(Object Relational Mapping)映射,是一种对象关系的映射,对象-关系映射(ORM)系统一般以中间件的形式存在,主要实现程序对象到关系数据库数据的映射。 而Automapper是一种实体转换关系的模型,AutoMapper是一个.NET的对象映射工具。主要作用是进行领域对象与模型(DTO)之间的转换、数据库查询结果映射至实体对象。 引入AutoMapper的包 在Services层中引用Nuget包,AutoMapper 和 AutoMapper.Extensions.Microsoft.DependencyInjection AutoMapper.Extensions.Microsoft

Automapper Projection with Linq OrderBy child property error

匿名 (未验证) 提交于 2019-12-03 02:33:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am having an issue using an AutoMapper (version 5.1.1) projection combined with a Linq OrderBy Child property expression. I am using Entity Framework Core (version 1.0.0). I am getting the following error: "must be reducible node" My DTO objects are as follows public class OrganizationViewModel { public virtual int Id { get; set; } [Display(Name = "Organization Name")] public virtual string Name { get; set; } public virtual bool Active { get; set; } public virtual int OrganizationGroupId { get; set; } public virtual string

Automapper complex types mapping exception

匿名 (未验证) 提交于 2019-12-03 02:33:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to implement the AutoMapper for a new module. I have the MVC model at the web site, I'm working on, and it looks like this: public class MvcModel { public Params Params { get; set; } public Steps Steps { get; set; } } public class Params { public int? RequestId { get; set; } public bool NewClient { get; set; } } public class Steps { public Step1 Step1 { get; set; } public Step2 Step2 { get; set; } public Step3 Step3 { get; set; } } public class Step1 { public int Name { get; set; } } public class Step2 { public int Phone { get;

Automapper - Mapper already initialized error

匿名 (未验证) 提交于 2019-12-03 02:30:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using AutoMapper 6.2.0 in my ASP.NET MVC 5 application. When I call my view through controller it shows all things right. But, when I refresh that view, Visual Studio shows an error: System.InvalidOperationException: 'Mapper already initialized. You must call Initialize once per application domain/process.' I am using AutoMapper only in one controller. Not made any configuration in any place yet nor used AutoMapper in any other service or controller. My controller: public class StudentsController : Controller { private DataContext db =

automapper Missing type map configuration or unsupported mapping.?

匿名 (未验证) 提交于 2019-12-03 02:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: ERROR Missing type map configuration or unsupported mapping. Mapping types: Cities_C391BA93C06F35100522AFBFA8F6BF3823972C9E97D5A49783829A4E90A03F00 -> IEnumerable`1 System.Data.Entity.DynamicProxies.Cities_C391BA93C06F35100522AFBFA8F6BF3823972C9E97D5A49783829A4E90A03F00 -> System.Collections.Generic.IEnumerable`1[[OsosPlus2.Core.DataAccess.Cities, OsosPlus2.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] Destination path: CustomerViewModel.Cities.Cities Source value: System.Data.Entity.DynamicProxies.Cities

AutoMapper - Deep level mapping

匿名 (未验证) 提交于 2019-12-03 02:14:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to map objects with multi-level members: these are the classes: public class Father { public int Id { get; set; } public Son Son { get; set; } } public class FatherModel { public int Id { get; set; } public int SonId { get; set; } } public class Son { public int Id { get; set; } } This is how I try automap it: AutoMapper.Mapper.CreateMap<FatherModel , Father>() .ForMember(dest => dest.Son.Id, opt => opt.MapFrom(src => src.SonId)); this is the exception that I get: Expression 'dest => Convert(dest.Son.Id)' must resolve to top

AutoMapper convert from multiple sources

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Let's say I have two model classes: public class People { public string FirstName {get;set;} public string LastName {get;set;} } Also have a class Phone: public class Phone { public string Number {get;set;} } And I want to convert to a PeoplePhoneDto like this: public class PeoplePhoneDto { public string FirstName {get;set;} public string LastName {get;set;} public string PhoneNumber {get;set;} } Let's say in my controller I have: var people = repository.GetPeople(1); var phone = repository.GetPhone(4); // normally, without automapper I

Using Profiles in Automapper to map the same types with different logic

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using AutoMapper in my ASP.NET MVC website to map my database objects to ViewModel objects and I am trying to use several profiles to map the same types, but using another logic. I had the idea of doing so by reading Matt's blog post where he says: The really key part is the AutoMapper configuration profile. You can group configurations with profiles. Maybe in one profile you format dates in one way, in another profile you format dates in another way. I’m just using one profile here. So I created a profile for one case: public class