我无法使用以下内容,其中array是CustomerContract的数组:
Mapper.Map<IEnumerable<Customer>>(array);
Mapper.Map<IEnumerable<CustomerContract>, IEnumerable<Customer>>(array);
Mapper.Map<Array, List<Customer>>(array);
Run Code Online (Sandbox Code Playgroud)
在我看来,第一个例子应该足够了,但我无法工作.我已经阅读了automapper的配置wiki(https://github.com/AutoMapper/AutoMapper/wiki/Configuration),但我不明白为什么这是必要的.Automapper所需的一切都在命令中定义.它是哪种类型(对象和它是一个列表),以及我希望它映射到哪个对象.
我只是不了解Automapper的核心概念?
我的例外听起来像这样:
缺少类型映射配置或不支持的映射.
映射类型:\ r \nCustomerContract - > Customer\r \nStimline.Xplorer.Repository.CustomerService.CustomerContract - > Stimline.Xplorer.BusinessObjects.Customer
目标路径:List`1 [0]
源值:Stimline.Xplorer.Repository.CustomerService .CustomerContract
我有一种情况,我需要根据某些条件将单个属性映射为多个源属性的组合.
目的地:
public class Email
{
public Email() {
EmailRecipient = new List<EmailRecipient>();
}
public string Subject{get; set;}
public string Body {get; set;}
public virtual ICollection<EmailRecipient> EmailRecipient { get; set; }
}
public class EmailRecipient
{
public int EmaiId { get; set; }
public string RecipientEmailAddress { get; set; }
public int RecipientEmailTypeId { get; set; }
public virtual Email Email { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
资源:
public class EmailViewModel
{
public List<EmailRecipientViewModel> To { get; set; }
public List<EmailRecipientViewModel> …
Run Code Online (Sandbox Code Playgroud) 如果我用一个(虚构的)例子解释这个问题,这可能是最简单的.
我有一个包含Order对象的nHibernate模型.此Order对象包含OrderItem nHibernate对象的集合.
我有一个Order ViewModel,它包含OrderItemDto的集合(实际上只是POCO).
在我的UI中,我显示了一个订单对象和一个OrderItems列表,它们在列表中是可编辑的.模型绑定负责填充OrderViewModel和OrderItemDto集合.
这是我的模特
namespace nHibernateModel
{
public class Order
{
private readonly IList<OrderItem> _OrderItems;
public int Id { get; set; }
public Order()
{
_OrderItems = new List<OrderItem>();
}
public IList<OrderItem> OrderItems
{
/* Setter does not exist to satisfy CA2227*/
get
{
return _OrderItems;
}
}
public void AddOrderItem(OrderItem orderItem)
{
//Set bi-directional relationships
if (_OrderItems.Contains(orderItem))
return;
orderItem.Order = this;
_OrderItems.Add(orderItem);
}
}
public class OrderItem
{
private readonly Order _Order;
public int Id { …
Run Code Online (Sandbox Code Playgroud) 我是Automapper的新手.
我已将Nuget包 - Automapper添加到我的Manager(BLL)和DAL层.
现在,下面是相关的东西:
以下是管理器库的声明,它给了我例外:
this.dataRepository.Update(Mapper.Map<StudentMaster>(studentDTO));
Run Code Online (Sandbox Code Playgroud)
例外情况如下:
缺少类型映射配置或不支持的映射.
映射类型:
studentDTO - >
StudentMaster Admin.App.DTO.studentDTO-> Admin.App.DAL.StudentMaster
如果选择/在EF上查询,它正在工作并能够使用
.Project().To<TReturn>()
Run Code Online (Sandbox Code Playgroud)
我写了一个Autoconfiguration.cs
文件如下:
public static class AutoMapperConfiguration
{
public static void Configure()
{
ConfigureStudentMasterMaps();
}
private static void ConfigureStudentMasterMaps()
{
Mapper.CreateMap<StudentMaster, studentDTO>();
}
}
Run Code Online (Sandbox Code Playgroud)
注意:
实体 - StudentMaster
(模型)实体StudentDTO
都具有相同的属性.
请指导我如何解决此问题.
谢谢
我在源对象和目标对象之间遇到了AutoMapper的挑战.我会尝试解释这种情况.在我的src对象上,我有一个字符串,根据它的长度,它应该映射到我的目标对象的多个属性.
class source
{
public int Id {get; set;}
/* some other properties */
public string Value {get; set;}
}
class destination
{
public int Id {get; set;}
/* some other properties with the same name as the source */
public string Value1 {get; set;}
public string Value2 {get; set;}
public string Value3 {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
预期的最大长度为30个字符(可能小于仅映射到两个属性或一个属性的字符).因此,每10个将映射到每个目标属性.我试图使用AutoMapper中的ResolveUsing方法,但是没有办法让函数知道我应该带回哪个段.所以我想忽略这些属性的映射,并在Automapper完成其他属性的工作后手动执行此操作
我知道有些问题类似于这个问题,但据我所知(并且测试),所提供的解决方案似乎都不合适,所以这里有.
我想知道是否可以展平/非规范化对象层次结构,以便使用AutoMapper将具有嵌套属性列表的实例映射到某个目标类型的列表.
我有一个类似于的源类
资料来源:
public class DistributionInformation
{
public string Streetname;
public RouteInformation[] Routes;
}
public class RouteInformation
{
public int RouteNumber;
public string RouteDescription;
}
Run Code Online (Sandbox Code Playgroud)
目的地:
public class DenormDistributionInfo
{
public string Streetname;
public int RouteNumber;
public string RouteDescription;
}
Run Code Online (Sandbox Code Playgroud)
所以我想将两个源映射到非规范化目标DenormDistributionInfo的列表.
即:
IEnumerable<DenormDistributionInfo> result = Mapper.Map(distributionInformationInstance);
Run Code Online (Sandbox Code Playgroud)
使用AutoMapper是可行/可行的,还是应该"手动"放弃和反规范?
我收到以下错误,只有当多个用户点击同一个按钮时才会出现此错误.任何帮助/想法将非常感激:
System.InvalidOperationException:集合已被修改; 枚举操作可能无法执行.生成:2015年6月10日星期三07:29:06 GMT
AutoMapper.AutoMapperMappingException:
映射类型:User - > User ApplicationSecurityManager.Service.User - > ApplicationSecurityManager.Models.User
目标路径:用户
源值:ApplicationSecurityManager.Service.User ---> System.InvalidOperationException:集合被修改; 枚举操作可能无法执行.在
1.Enumerator.MoveNextRare() at AutoMapper.TypeMap.<get_AfterMap>b__1(Object src, Object dest) at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper) at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper) at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) --- End of inner exception stack trace --- at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) at AutoMapper.MappingEngine.Map[TDestination](Object source, Action
SystemSecurityManager.Infrastructure.ApplicationSecurityAttribute.OnAuthorization(AuthorizationContext filterContext)的ApplicationSecurityManager.UserManager.IsAuthenticated()的ApplicationSecurityManager.UserManager.get_AuthenticatedUser()处的ApplicationSecurityManager.UserManager.LoadUser(String username)处的System.Collections.Generic.List 1 opts) System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext,IList1 filters, ActionDescriptor actionDescriptor) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult
1.Begin(AsyncCallback回调,对象状态,Int32超时),位于System.Web.Mvc.Async.AsyncResultWrapper.Begin [TResult](AsyncCallback回调,对象状态,BeginInvokeDelegate) System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1 endDelegate, Object tag, Int32 timeout) at …
我必须用automapper创建一个Mapping.
Public class Source
{
public string Id;
public string Firstname;
public string Lastname;
}
Run Code Online (Sandbox Code Playgroud)
目的地是
Public class Destination
{
public string Id;
public Person[] persons;
}
Run Code Online (Sandbox Code Playgroud)
人类是
Public class Person
{
public string FirstName;
public string LastName;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建映射
AutoMapper.Mapper.CreateMap<Source, Destination>();
Run Code Online (Sandbox Code Playgroud)
但我不知道如何将Firstname,Lastname映射到对象Person的数组.
在AutoMapper中使用自定义类型转换器(ITypeConverter)时,如果源值为null
,则似乎不会输入类型转换器代码,例如:
Mapper.CreateMap<string, Enum>().ConvertUsing<EnumConverter>();
Assert.AreEqual(Enum.Value1, Mapper.Map<Enum>("StringValue1"));
Assert.AreEqual(Enum.Value1, Mapper.Map<Enum>(null);
Assert.AreEqual(Enum.Value1, Mapper.Map<Enum?>(null);
Run Code Online (Sandbox Code Playgroud)
类型转换器看起来像:
public class EnumConvertor: ITypeConverter<string, Enum>
{
public Enum Convert(ResolutionContext context)
{
string value = (string) context.SourceValue;
switch (value)
{
case "StringValue2":
return Enum.Value2;
case "StringValue3":
return Enum.Value3;
case "StringValue1":
default:
return Enum.Value1;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在最后两个案例中,结果如下:
Assert.AreEqual(Enum.Value1, Mapper.Map<Enum>(null);
Run Code Online (Sandbox Code Playgroud)
0 - 不是有效的枚举值
Assert.AreEqual(Enum.Value1, Mapper.Map<Enum?>(null);
Run Code Online (Sandbox Code Playgroud)
空值
从调试到测试,在这两种情况下,自定义TypeConverter永远不会被击中,似乎AutoMapper在映射器中有一些初始检查要映射而不需要求助于TypeConverter?
如果我指定一个空字符串(""),则测试按预期工作.
鉴于这两个对象
public class UserModel
{
public string Name {get;set;}
public IList<RoleModel> Roles {get;set;}
}
public class UserViewModel
{
public string Name {get;set;}
public IList<RoleViewModel> Roles {get;set;} // notice the ViewModel
}
Run Code Online (Sandbox Code Playgroud)
这是做映射最优化的方式,或者是AutoMapper能够映射Roles
到Roles
自身?
App配置
Mapper.CreateMap<UserModel, UserViewModel>()
.ForMember(dest => dest.Roles, opt => opt.MapFrom(src => src.Roles));
Mapper.CreateMap<UserViewModel, UserModel>()
.ForMember(dest => dest.Roles, opt => opt.MapFrom(src => src.Roles));
Run Code Online (Sandbox Code Playgroud)
履行
_userRepository.Create(Mapper.Map<UserModel>(someUserViewModelWithRolesAttached);
Run Code Online (Sandbox Code Playgroud) automapper-3 ×10
c# ×6
automapper ×5
automapper-2 ×2
asp.net-mvc ×1
automapping ×1
unit-testing ×1
vb.net ×1