コードファーストを使用してモデルを生成するEF6 MVCアプリケーションがあります。私はモデルとそのモデルのviewmodel表現の間でAutoMapperを使用してマッピングを作成しようとしています。マップされたオブジェクトの1つにEF6が挿入されていないAutoMapper
私はエラーはありませんが、結果オブジェクトにPTCredentialオブジェクトの詳細がありません。具体的には、PTCredential内にネストされたPTCredentialオブジェクトがありません。 私は無視がこれを引き起こしているとは思わないが、私はそれを固定することはできない。私はAutoMapperの他の投稿からいくつかの提案を試みましたが、何も有用であるとは証明されていません。
誰でも私がこの状況でAutoMapperをうまく使用するためにできることをいくつか助けてくれますか?ここで
は私がマッパーを設定し、初期化していますどのようにするコードです:ここで
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);
は私のコード最初のモデルである:ここでは
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; }
}
私はautomapperに慣れていません...しかし、あなたはナビゲーションプロパティをどこに入れますか?通常のDbSet呼び出しでは、このプロパティは列挙されるか、対応するエンティティがすでにコンテキストに入るまでnullにする必要があります。 – DevilSuichiro
「特にPTCredential内にネストされたPTCredentialオブジェクトがありません」 - これは誤字ですか? PTCredentialAssignmentViewModelからPTCredentialAssignmentViewModelを紛失した場合は、それを無視するように指示します。 –
@ Steve-Greeneあなたの提案をお寄せいただきありがとうございます、時々あなたは木々の中で森を見ることができません。投稿前にデータモデルをよく調べていたと思っていましたが、誤植を指摘したときに私は戻ってきて、私が見逃していたものを見ることができました。私は他のオブジェクトの1つがマッピングであることに気付いて、正しいマッピングを実行して正しいオブジェクトを取得しないようにしていました。ご協力いただきありがとうございます! – silverbullettruck2001