草庐IT

automapper

全部标签

c# - AutoMapper 映射如果不为空,否则自定义转换

这是我的代码:Mapper.CreateMap().ForMember(dest=>dest.Bar,opt=>opt.MapFrom(src=>src.Bar==null?newBarViewModel():src.Bar))基本上,“BarViewModel”有一个无参数的构造函数,它在类中设置属性。所以我想对AutoMapper说:Ifthevalueisnull,thenusethectorfortheclass.otherwiseusethemappingyouhaveinplace以上是给我一个C#编译器错误。我猜类型转换也行不通。那么有没有AutoMapper技巧可以做到

c# - 使用 AutoMapper 映射集合

我正在尝试将数组映射到ICollection类型.基本上我希望能够做到:Mapper.CreateMap();在哪里Y是Collection有什么想法吗? 最佳答案 您不需要为集合设置映射,只需设置元素类型。所以只是:Mapper.CreateMap();Mapper.Map>(objectToMap);查看此处了解更多信息:http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays&referringTitle=Home

c# - 在 Automapper 中使用 Profiles 来映射具有不同逻辑的相同类型

我在我的ASP.NETMVC网站中使用AutoMapper将我的数据库对象映射到ViewModel对象,我正在尝试使用多个配置文件来映射相同的类型,但使用的是另一种逻辑。我是通过阅读Matt'sblogpost想到这样做的他说:ThereallykeypartistheAutoMapperconfigurationprofile.Youcangroupconfigurationswithprofiles.Maybeinoneprofileyouformatdatesinoneway,inanotherprofileyouformatdatesinanotherway.I’mjustus

c# - 如何在 AutoMapper 中配置条件映射?

假设我有以下实体(类)publicclassTarget{publicstringValue;}publicclassSource{publicstringValue1;publicstringValue2;}现在我想配置自动映射,如果Value1以“A”开头,则将Value1映射到Value,否则我想将Value2映射到Value。这是我目前所拥有的:Mapper.CreateMap().ForMember(t=>t.Value,o=>{o.Condition(s=>s.Value1.StartsWith("A"));o.MapFrom(s=>s.Value1);>})然而,我仍然无

c# - 如何通过 AutoMapper 将匿名对象映射到类?

我有一个实体:publicclassTag{publicintId{get;set;}publicstringWord{get;set;}//otherproperties...//andacollectionofblogposts:publicICollectionPosts{get;set;}}和一个模型:publicclassTagModel{publicintId{get;set;}publicstringWord{get;set;}//otherproperties...//andacollectionofblogposts:publicintPostsCount{get;s

c# - AutoMapper 和继承 - 如何映射?

有这个场景:publicclassBase{publicstringName;}publicClassClassA:Base{publicint32Number;}publicClassClassB:Base{publicstringDescription;}publicClassDTO{publicstringName;publicint32Number;publicstringDescription;}我有一个IList我的map是:AutoMapper.Mapper.CreateMap,IList>().ForMember(dest=>dest.Number,opt=>opt.I

c# - Automapper 无法映射到 IEnumerable

我有两个这样的类:publicclassSentEmailAttachment:ISentEmailAttachment{publicSentEmailAttachment();publicstringFileName{get;set;}publicstringID{get;set;}publicstringSentEmailID{get;set;}publicstringStorageService{get;set;}publicstringStorageServiceFileID{get;set;}}和publicclassSentEmailAttachmentItem:ISent

c# - AutoMapper map 子属性也定义了 map

我有以下域对象:publicclassDomainClass{publicintId{get;set;}publicstringA{get;set;}publicstringB{get;set;}}我有以下两个要映射到的对象:publicclassParent{publicintId{get;set;}publicstringA{get;set;}publicChildChild{get;set;}}publicclassChild{publicintId{get;set;}publicstringB{get;set;}}我设置了以下map:Mapper.CreateMap();Map

c# - NuGet 包管理器 : 'AutoMapper' already has a dependency defined for 'Microsoft.CSharp'

我尝试将AutoMapper添加为在VisualStudioPremium2012上使用NuGet的项目的依赖项,但它失败了。它说:Operationfailed'AutoMapper'alreadyhasadependencydefinedfor'Microsoft.CSharp'.我可以添加其他依赖项。我正在使用VS2012的最新版本的包管理器:NuGetPackageManager2.8.60318.667有什么我应该检查的想法吗? 最佳答案 问题是您的NuGet包管理器太旧了。你需要NuGet2.12因为它支持AutoMap

c# - 如何在 ASP.NET Core 中设置 Automapper

我是.NET的新手,因此我决定着手解决.NETCore而不是学习“旧方法”。我找到了一篇关于settingupAutoMapperfor.NETCorehere的详细文章,但是对于新手有更简单的演练吗? 最佳答案 我想通了!详情如下:通过NuGet将主要的AutoMapper包添加到您的解决方案中.通过NuGet将AutoMapper依赖注入(inject)包添加到您的解决方案中.为映射配置文件创建一个新类。(我在名为MappingProfile.cs的主解决方案目录中创建了一个类,并添加了以下代码。)我将使用User和UserDt