如何使用automapper在我的模型类中映射ICollection属性。
以下是示例:
Mapper.CreateMap<Planiranje.Planning.Data.User, Model.Users>()
.ForMember(dst => dst.UserId, opts => opts.MapFrom(src => src.UserId))
.ForMember(dst => dst.Username, opts => opts.MapFrom(src => src.Username))
.ForMember(dst => dst.Firstname, opts => opts.MapFrom(src => src.Firstname))
.ForMember(dst => dst.Lastname, opts => opts.MapFrom(src => src.Lastname))
.ForMember(dst => dst.Email, opts => opts.MapFrom(src => src.Email))
.ForMember(dst => dst.Segment, opts => opts.MapFrom(src => src.Segment))
.ForMember(dst => dst.Groups, opts => opts.MapFrom(src => src.Groups));
其中,群组属于ICollection类型。
通过此映射,我收到以下错误
The following property on System.Text.RegularExpressions.Group cannot be mapped:
Groups
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type System.Text.RegularExpressions.Group.
Context:
Mapping to property Groups from Planiranje.Planning.Data.Group to System.Text.RegularExpressions.Group
Mapping to property Groups from System.Collections.Generic.ICollection`1[[Planiranje.Planning.Data.Group, Planiranje, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.ICollection`1[[System.Text.RegularExpressions.Group, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
Mapping from type Planiranje.Planning.Data.User to Planiranje.Model.Users
Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.
在我摆脱Syste.Text.RegularExpression后,我收到以下错误消息
The following property on Planiranje.Model.Groups cannot be mapped:
Groups
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type Planiranje.Model.Groups.
Context:
Mapping to property Groups from Planiranje.Planning.Data.Group to Planiranje.Model.Groups
Mapping to property Groups from System.Collections.Generic.ICollection`1[[Planiranje.Planning.Data.Group, Planiranje, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.ICollection`1[[Planiranje.Model.Groups, Planiranje, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Mapping from type Planiranje.Planning.Data.User to Planiranje.Model.Users
Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown.
此外,这是我的源/目标类 来源类
namespace Planiranje.Planning.Data
{
using System;
using System.Collections.Generic;
public partial class User
{
public User()
{
this.Groups = new HashSet<Group>();
}
public int UserId { get; set; }
public string Username { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Email { get; set; }
public string Segment { get; set; }
public virtual ICollection<Group> Groups { get; set; }
}
}
namespace Planiranje.Planning.Data
{
using System;
using System.Collections.Generic;
public partial class Group
{
public Group()
{
this.Users = new HashSet<User>();
}
public int GroupId { get; set; }
public string GroupName { get; set; }
public virtual ICollection<User> Users { get; set; }
}
}
和目的地类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
namespace Planiranje.Model
{
public class Users : ModelBase,IEntity
{
private int _userId;
public int UserId
{
get
{
return _userId;
}
set
{
_userId = value;
RaisePropertyChanged("UserId");
}
}
private string _username;
public string Username
{
get
{
return _username;
}
set
{
_username = value;
RaisePropertyChanged("Username");
}
}
private string _firstname;
public string Firstname
{
get
{
return _firstname;
}
set
{
_firstname = value;
RaisePropertyChanged("Firstname");
}
}
private string _lastname;
public string Lastname
{
get
{
return _lastname;
}
set
{
_lastname = value;
RaisePropertyChanged("Lastname");
}
}
private string _email;
public string Email
{
get
{
return _email;
}
set
{
_email = value;
RaisePropertyChanged("Email");
}
}
private string _segment;
public string Segment
{
get
{
return _segment;
}
set
{
_segment = value;
RaisePropertyChanged("Segment");
}
}
private IList<Groups> _groups;
public virtual IList<Groups> Groups
{
get
{
return _groups;
}
set
{
_groups = value;
RaisePropertyChanged("Groups");
}
}
public ModelEntityState ModelEntityState { get; set;}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
namespace Planiranje.Model
{
public class Groups :ModelBase, IEntity
{
private int _groupId;
public int GroupId
{
get
{
return _groupId;
}
set
{
_groupId = value;
RaisePropertyChanged("UserId");
}
}
private string _groupname;
public string GroupName
{
get
{
return _groupname;
}
set
{
_groupname = value;
RaisePropertyChanged("GroupName");
}
}
private ICollection<Users> _users;
public virtual ICollection<Users> Users
{
get
{
return _users;
}
set
{
_users = value;
RaisePropertyChanged("Users");
}
}
public ModelEntityState ModelEntityState { get; set; }
}
}
答案 0 :(得分:0)
AutoMapper会很好地映射ICollection组。那不是你的问题。 异常描述的问题是:
Planiranje.Planning.Data.Group
System.Text.RegularExpressions.Group
我猜目标属性类型是错误的。
我想您的文件Model.Users.cs
顶部有一个using System.Text.RegularExpressions
。
如果是这样,您最好的解决方法可能是添加
using Group=Planiranje.Planning.Data.Group;
到Model.Users.cs
答案 1 :(得分:0)
我认为问题在于你的映射
Mapper.CreateMap<Planiranje.Planning.Data.User, Model.Users>()
您还必须定义映射
Mapper.CreateMap<Planiranje.Planning.Data.Group,Planiranje.Model.Groups>()
你必须以递归的方式为所有你的类所依赖的类型继续这样做。
我看到当有完整的类系列时,AutoMapper配置会变得非常大。