问题描述
我有一个使用Code First生成模型的EF6 MVC应用.我正在尝试使用AutoMapper在模型和该模型的视图模型表示之间创建映射.但是,当我执行映射时,会收到错误消息:
I have an EF6 MVC app using Code First to generate the models. I am trying to create a mapping using AutoMapper between the model and a view model representation of that model. However, when I do the mapping I receive the error:
我肯定是在初始化Mapper,但是我无法弄清为什么配置失败.在这种情况下,谁能提供一些帮助我成功使用AutoMapper的帮助?
I am definitely initializing the Mapper, but I am not able to figure out why the configuration is failing. Could anyone provide some assistance on what I can do to successfully use AutoMapper in this situation?
以下是我如何配置和初始化Mapper的代码:
Here is the code for how I am configuring and initializing the Mapper:
var pTConnections = _db.PTConnections.Include(p => p.PTConnectionClass).ToList();
Mapper.Initialize(cfg =>
{
cfg.CreateMap<PTConnection, PTConnectionViewModel>()
.AfterMap((s, d) => { foreach (var i in d.PTCredentialAssignments) i.PTConnection = d; });
cfg.CreateMap<PTCredentialAssignment, PTCredentialAssignmentViewModel>()
.ForMember(m => m.PTCredential, opt => opt.Ignore())
.ForMember(m => m.PTConnection, opt => opt.Ignore());
cfg.CreateMap<PTVendor, PTVendorViewModel>()
.ForMember(m => m.PTCredentials, opt => opt.Ignore())
.ForMember(m => m.PTConnections, opt => opt.Ignore());
cfg.CreateMap<PTCredential, PTCredentialViewModel>();
cfg.CreateMap<PTConnectionClass, PTConnectionClassViewModel>()
.ForMember(m => m.PTConnections, opt => opt.Ignore());
cfg.CreateMap<PTConnectionContactAssignment, PTConnectionContactAssignmentViewModel>()
.ForMember(m => m.PTConnection, opt => opt.Ignore());
});
var dest = Mapper.Map<List<PTConnection>, List<PTConnectionViewModel>>(pTConnections);
这是我的代码优先模型:
Here is my Code First Model:
public partial class PTConnection
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PTConnection()
{
PTConnectionAssignments = new HashSet<PTConnectionAssignment>();
PTCredentialAssignments = new HashSet<PTCredentialAssignment>();
PTConnectionContactAssignments = new HashSet<PTConnectionContactAssignment>();
}
[Key]
public int PTCID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(255)]
public string ConnectionName { get; set; }
[Required]
[StringLength(100)]
public string Host { get; set; }
public int Port { get; set; }
public string Comment { get; set; }
public int PTCCID { get; set; }
public bool? IsDisabled { get; set; }
public int PTVID { get; set; }
public virtual PTConnectionClass PTConnectionClass { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTConnectionAssignment> PTConnectionAssignments { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTCredentialAssignment> PTCredentialAssignments { get; set; }
public virtual PTVendor PTVendor { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTConnectionContactAssignment> PTConnectionContactAssignments { get; set; }
}
public partial class PTCredential
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PTCredential()
{
PTCredentialAssignments = new HashSet<PTCredentialAssignment>();
PTCredentialContactAssignments = new HashSet<PTCredentialContactAssignment>();
}
[Key]
public int PTCRID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(100)]
public string CredUsername { get; set; }
[Required]
[StringLength(500)]
public string CredPassword { get; set; }
public string Directory { get; set; }
public bool? IsDisabled { get; set; }
public string Comments { get; set; }
public int? PTVID { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTCredentialAssignment> PTCredentialAssignments { get; set; }
public virtual PTVendor PTVendor { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTCredentialContactAssignment> PTCredentialContactAssignments { get; set; }
}
public partial class PTVendor
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PTVendor()
{
PTConnections = new HashSet<PTConnection>();
PTCredentials = new HashSet<PTCredential>();
PTIneligableCredentialVendorAssignments = new HashSet<PTIneligableCredentialVendorAssignment>();
PTVendorAssignments = new HashSet<PTVendorAssignment>();
PTVendorContactAssignments = new HashSet<PTVendorContactAssignment>();
}
[Key]
public int PTVID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(50), Display(Name="Vendor Name")]
public string Name { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTConnection> PTConnections { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTCredential> PTCredentials { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTIneligableCredentialVendorAssignment> PTIneligableCredentialVendorAssignments { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTVendorAssignment> PTVendorAssignments { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTVendorContactAssignment> PTVendorContactAssignments { get; set; }
}
[Table("PTCredentialAssignment")]
public partial class PTCredentialAssignment
{
[Key]
public int PTCRAID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
public int PTCID { get; set; }
public int PTCRID { get; set; }
public virtual PTConnection PTConnection { get; set; }
public virtual PTCredential PTCredential { get; set; }
}
[Table("PTConnectionClass")]
public partial class PTConnectionClass
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PTConnectionClass()
{
PTConnections = new HashSet<PTConnection>();
}
[Key]
public int PTCCID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(50)]
public string Class { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PTConnection> PTConnections { get; set; }
}
[Table("PTConnectionContactAssignment")]
public partial class PTConnectionContactAssignment
{
[Key]
public int PTCCAID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
public int PTCID { get; set; }
[Required]
[StringLength(60)]
public string FirstName { get; set; }
[Required]
[StringLength(60)]
public string LastName { get; set; }
[Required]
[StringLength(10)]
public string PhoneNumber { get; set; }
[StringLength(20)]
public string Extension { get; set; }
[StringLength(100)]
public string Email { get; set; }
[StringLength(60)]
public string Title { get; set; }
public string Comment { get; set; }
public virtual PTConnection PTConnection { get; set; }
}
这是我的ViewModel:
Here is my ViewModel:
public partial class PTConnectionViewModel
{
[Key]
public int PTCID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(255)]
public string ConnectionName { get; set; }
[Required]
[StringLength(100)]
public string Host { get; set; }
public int Port { get; set; }
public string Comment { get; set; }
public int PTCCID { get; set; }
public bool? IsDisabled { get; set; }
public int PTVID { get; set; }
public virtual PTConnectionClassViewModel PTConnectionClass { get; set; }
public virtual ICollection<PTConnectionAssignmentViewModel> PTConnectionAssignments { get; set; }
public virtual ICollection<PTCredentialAssignmentViewModel> PTCredentialAssignments { get; set; }
public virtual PTVendorViewModel PTVendor { get; set; }
public virtual ICollection<PTConnectionContactAssignmentViewModel> PTConnectionContactAssignments { get; set; }
}
public partial class PTVendorViewModel
{
[Key]
public int PTVID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(50), Display(Name="Vendor Name")]
public string Name { get; set; }
public virtual ICollection<PTConnectionViewModel> PTConnections { get; set; }
public virtual ICollection<PTCredentialViewModel> PTCredentials { get; set; }
}
public partial class PTCredentialViewModel
{
[Key]
public int PTCRID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(100)]
public string CredUsername { get; set; }
[Required]
[StringLength(500)]
public string CredPassword { get; set; }
public string Directory { get; set; }
public bool? IsDisabled { get; set; }
public string Comments { get; set; }
public int? PTVID { get; set; }
public virtual ICollection<PTCredentialAssignmentViewModel> PTCredentialAssignments { get; set; }
public virtual PTVendorViewModel PTVendor { get; set; }
public virtual ICollection<PTCredentialContactAssignmentViewModel> PTCredentialContactAssignments { get; set; }
}
public partial class PTCredentialAssignmentViewModel
{
[Key]
public int PTCRAID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
public int PTCID { get; set; }
public int PTCRID { get; set; }
public virtual PTConnectionViewModel PTConnection { get; set; }
public virtual PTCredentialViewModel PTCredential { get; set; }
}
public partial class PTConnectionClassViewModel
{
[Key]
public int PTCCID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
[Required]
[StringLength(50)]
public string Class { get; set; }
public virtual ICollection<PTConnectionViewModel> PTConnections { get; set; }
}
public partial class PTConnectionContactAssignmentViewModel
{
[Key]
public int PTCCAID { get; set; }
[Column(TypeName = "datetime2")]
public DateTime ModifiedDate { get; set; }
[Required]
[StringLength(500)]
public string ModifiedBy { get; set; }
public int PTCID { get; set; }
[Required]
[StringLength(60)]
public string FirstName { get; set; }
[Required]
[StringLength(60)]
public string LastName { get; set; }
[Required]
[StringLength(10)]
public string PhoneNumber { get; set; }
[StringLength(20)]
public string Extension { get; set; }
[StringLength(100)]
public string Email { get; set; }
[StringLength(60)]
public string Title { get; set; }
public string Comment { get; set; }
public virtual PTConnectionViewModel PTConnection { get; set; }
}
推荐答案
只需将地图从一种类型创建为另一种即可.不要将地图从一种类型的列表创建为另一种类型的列表. Automapper可以自动处理集合.
Just create the map from one type to another. Don't create the map from a List of one type to a list of another type. Automapper can automatically handle collections.
实际上,您是在告诉Automapper如何将List<A>
映射到List<B>
,而没有告诉Automapper如何将A
实际映射到B
.将您的注册更改为以下内容:
In effect, you're telling Automapper how to map a List<A>
to a List<B>
without telling Automapper how to actually map A
to B
. Change your registration to the following:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<PTConnection, PTConnectionViewModel>();
});
您仍然可以将List<PTConnection>
以及大多数其他集合类型映射到List<PTConnectionViewModel>
.
You can still map a List<PTConnection>
to a List<PTConnectionViewModel>
just fine, as well as most other collection types.
这篇关于具有实体框架的自动映射器,初始化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!