C# 使用EF的一对多关系不映射到使用AutoMapper的ViewModel

C# 使用EF的一对多关系不映射到使用AutoMapper的ViewModel,c#,entity-framework,automapper,C#,Entity Framework,Automapper,我试图将两个实体映射到它的ViewModels,但有一点是错误的,因为VisualStudio显示StackOveflowException 拥有EF6的实体: public partial class Users { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public User

我试图将两个实体映射到它的ViewModels,但有一点是错误的,因为VisualStudio显示StackOveflowException

拥有EF6的实体:

public partial class Users
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Users()
    {
        this.Mayorista = new HashSet<Mayorista>();
    }

    public int UserId { get; set; }
    public string Name { get; set; }
    public System.DateTime BirthDate { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public string Telephone { get; set; }
    public string Email { get; set; }
    public bool Active { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Mayorista> Mayorista { get; set; }
}


public partial class Mayorista
{
    public int idMayorista { get; set; }
    public int idUser { get; set; }
    public string nombre { get; set; }

    public virtual Users Users { get; set; }
}
公共部分类用户
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共用户()
{
this.Mayorista=newhashset();
}
public int UserId{get;set;}
公共字符串名称{get;set;}
public System.DateTime生日{get;set;}
公共字符串City{get;set;}
公共字符串国家{get;set;}
公用字符串电话{get;set;}
公共字符串电子邮件{get;set;}
公共bool活动{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection Mayorista{get;set;}
}
公共部分阶级市长
{
公共整数idMayorista{get;set;}
public int idUser{get;set;}
公共字符串nombre{get;set;}
公共虚拟用户用户{get;set;}
}
视图模型:

public class UsersViewModel
{
    public int UserId { get; set; }
    public string Name { get; set; }
    public System.DateTime BirthDate { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public string Telephone { get; set; }
    public string Email { get; set; }
    public bool Active { get; set; }
    public virtual ICollection<MayoristaViewModel> Mayorista { get; set; }
}


public class MayoristaViewModel
{
    public int idMayorista { get; set; }
    public int idUser { get; set; }
    public string nombre { get; set; }

    public virtual UsersViewModel Users { get; set; }
}
公共类UsersViewModel
{
public int UserId{get;set;}
公共字符串名称{get;set;}
public System.DateTime生日{get;set;}
公共字符串City{get;set;}
公共字符串国家{get;set;}
公用字符串电话{get;set;}
公共字符串电子邮件{get;set;}
公共bool活动{get;set;}
公共虚拟ICollection Mayorista{get;set;}
}
公共类市长视图模型
{
公共整数idMayorista{get;set;}
public int idUser{get;set;}
公共字符串nombre{get;set;}
公共虚拟用户viewmodel用户{get;set;}
}
使用Automapper实现:

var user = _service.GetById(id);

Mapper.Initialize(cfg => {
    cfg.CreateMap<Users, UsersViewModel>();
    cfg.CreateMap<Mayorista, MayoristaViewModel>();
});

var userView = Mapper.Map<Users, UsersViewModel>(user);
var user=\u service.GetById(id);
Mapper.Initialize(cfg=>{
CreateMap();
CreateMap();
});
var userView=Mapper.Map(用户);
映射器初始化是正确的,但是当我执行Mapper.Map时,VisualStudio显示StackOverFlowException


谢谢你

可能的复制我认为它有点不同,因为它是一对多的关系。可能,也可能不是。据我所知,问题在于Automapper不会记住它已经映射的实体,因此任何直接或间接的自引用数据结构都会导致这种异常。当然,我可能完全错了,但这是一个合理的假设。无论如何,这个问题有一些建议,你应该试着告诉我们为什么它们不适合你的具体情况。可能的重复我认为它有点不同,因为它是一对多的关系。可能,也可能不是。据我所知,问题在于Automapper不会记住它已经映射的实体,因此任何直接或间接的自引用数据结构都会导致这种异常。当然,我可能完全错了,但这是一个合理的假设。无论如何,这个问题有一些建议,你应该试着告诉我们为什么它们不适合你的具体情况。