Ich habe Code zuerst, dass die Verwendung Kind Eltern mit dem gleichen TypAutoMapper Karte Kind gleichen Typ
public class Comment
{
public int Id { get; set; }
public string Text { get; set; }
public int? ParentId { get; set; }
public virtual Comment Parent { get; set; }
public virtual IList<Comment> Child { get; set; }
}
Fluent API:
modelBuilder.Entity<Comment>().Property(c => c.ParentId).IsOptional();
modelBuilder.Entity<Comment>().HasMany(c => c.Child).WithOptional(c => c.Parent).HasForeignKey(c => c.ParentId);
Der in Entity Framework in Ordnung. Aber wenn ich versuche, es auf Automapper zu verwenden, werfe ich eine StackOverflowException.
AutoMapperConfig:
cfg.CreateMap<Comment, CommentDTO>().ForMember(d => d.Child, opt => opt.UseDestinationValue());
CommentDTO:
public class CommentDTO
{
public int Id { get; set; }
public string Text { get; set; }
public int? ParentId { get; set; }
public virtual CommentDTO Parent { get; set; }
public virtual IList<CommentDTO> Child { get; set; }
}
Controller:
Context.Comments.GetAll().AsNoTracking().ProjectTo<CommentDTO>().AsQueryable();
Vielen Dank für Ihre Hilfe und für den Tipp über die Namenskonvention. Leider bekomme ich mit ReverseMap auch den Fehler StackOverflowException, wenn ich meine Funktion Context.Comments.ProjectTo(); –
Sauron
Versuchen Sie es mit '.PreserveReferences()', wie ich es in meiner Antwort bearbeitet habe. – CodingYoshi
Tut mir leid, nichts zu tun. Ich denke, die StackOverflowException wird durch die Liste der Kind des gleichen Typs verursacht. Ähnliche Fragen hier http://stackoverflow.com/questions/37785124/automapper-map-child-list-with-from-same-type – Sauron