使用EF6的AutoMapper不会填充其中一个映射对象

时间:2016-09-12 19:33:41

标签: entity-framework automapper dto

我有一个使用Code First生成模型的EF6 MVC应用程序。我试图在模型和该模型的viewmodel表示之间使用AutoMapper创建映射。

我没有错误,但生成的对象缺少PTCredential对象的详细信息。具体来说,我错过了嵌套在PTCredential中的PTCredential对象。 我不相信忽略会造成这种情况,但我不能把它钉死。我已经尝试过AutoMapper上其他帖子的一些建议,但没有任何证据有用。

在这种情况下,有没有人可以帮助我成功使用AutoMapper?

以下是我如何配置和初始化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);

这是我的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:

   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; }
}

1 个答案:

答案 0 :(得分:0)

以下是使我的解决方案有效的映射:

       cfg.CreateMap<PTConnection, PTConnectionViewModel>()
            .AfterMap((s, d) => { foreach (var i in d.PTCredentialAssignments) i.PTConnection = d; });

        cfg.CreateMap<PTCredentialAssignment, PTCredentialAssignmentViewModel>()
            .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>()
            .ForMember(m => m.PTCredentialAssignments, opt => opt.Ignore())
            .ForMember(m => m.PTCredentialContactAssignments, opt => opt.Ignore());

        cfg.CreateMap<PTConnectionClass, PTConnectionClassViewModel>()
            .ForMember(m => m.PTConnections, opt => opt.Ignore());

        cfg.CreateMap<PTConnectionContactAssignment, PTConnectionContactAssignmentViewModel>()
            .ForMember(m => m.PTConnection, opt => opt.Ignore());