繁体   English   中英

EF6流利的一对一堆栈溢出

[英]EF6 fluent one-to-one stackoverflow

我已经阅读了很多文章,并且对我有所帮助,但是我仍然对使用实体框架6流畅定义的一对一关系(Employe和Adresse之间)有一个stackoverflow。 当我创建一个包含一些DTO的ViewModel新实例时,该错误会出现在AdresseDTO上。

我的普通物件

public class Personne
    {
        public int id { get; set; }
        public string civ { get; set; }
        public string nom { get; set; }
        public string prenom { get; set; }
        public string email { get; set; }
        public string tel1 { get; set; }
        public string tel2 { get; set; }
        public Boolean isFournisseur { get; set; }
        public Boolean isClient { get; set; }
        public Boolean isDeleted { get; set; }
        public virtual Adresse adresse { get; set; }
        public virtual Utilisateur utilisateur { get; set; }

        public Personne()
        {

        }
    }

 public class Employe : Personne
    {
        public virtual TEmploye typeEmploye { get; set; }
        public virtual List<AffectationService> affectationServices { get; set; }

        public Employe()
        {

        }
    }

public class AffectationService
{
    public int id { get; set; }

    public virtual Employe employe { get; set; }
    public virtual Service service { get; set; }
    public virtual Droit groupe { get; set; }
    public Boolean isPrincipal { get; set; }
}

 public class TEmploye
{
    public int id { get; set; }
    public string libe { get; set; }
    public TEmploye()
    {

    }
}

 public class Adresse
    {
        public int id { get; set; }
        public string numRue { get; set; }
        public string nomRue { get; set; }
        public string codePostal { get; set; }
        public string ville { get; set; }
        public string pays { get; set; }
        public virtual Personne personne { get; set; }

        public Adresse()
        {

        }
    }

我的DTO

 public class PersonneDTO
    {
        public int id { get; set; }

        public bool isDeleted { get; set; }

        public string civ { get; set; }

        public string nom { get; set; }

        public string prenom { get; set; }

        public string email { get; set; }

        public string tel1 { get; set; }

        public string tel2 { get; set; }
        public AdresseDTO adresse { get; set; }
        public UtilisateurDTO utilisateur { get; set; }

        public PersonneDTO()
        {
            adresse = new AdresseDTO();
            utilisateur = new UtilisateurDTO();
        }
    }

     public class EmployeDTO : PersonneDTO
    {
        public virtual TEmployeDTO typeEmploye { get; set; }

        public virtual List<AffectationServiceDTO> affectationServices { get; set; }

        public EmployeDTO()
        {
            affectationServices = new List<AffectationServiceDTO>();
            typeEmploye = new TEmployeDTO();
        }

    }

    public class TEmployeDTO
    {
         public int id { get; set; }
         public string libe { get; set; }

         public TEmployeDTO()
         {

         }
    }

public class AffectationServiceDTO
{
    public int id { get; set; }

    public virtual EmployeDTO employe { get; set; }

    public virtual ServiceDTO service { get; set; }

    public virtual DroitDTO groupe { get; set; }

    public Boolean isPrincipal { get; set; }

    public AffectationServiceDTO()
    {
        service = new ServiceDTO();
        groupe = new DroitDTO();
        employe = new EmployeDTO();
    }

}

     public class AdresseDTO
    {

        public int id { get; set; }

        public string numRue { get; set; }

        public string nomRue { get; set; }

        public string codePostal { get; set; }

        public string ville { get; set; }

        public string pays { get; set; }

        public AdresseDTO()
        {
        }
    }

如何将DTO与普通对象映射

//DomainToViewModel
CreateMap<Adresse, AdresseDTO>().MaxDepth(1);
CreateMap<Personne, PersonneDTO>().MaxDepth(1);
CreateMap<Employe, EmployeDTO>().MaxDepth(1);
CreateMap<AffectationService, AffectationServiceDTO>().MaxDepth(1);
CreateMap<TEmploye, TEmployeDTO>().MaxDepth(1);

//ViewModelToDomain
CreateMap<AdresseDTO, Adresse>().MaxDepth(1)
    .ForMember(g => g.id, map => map.MapFrom(vm => vm.id))
    .ForMember(g => g.codePostal, map => map.MapFrom(vm => vm.codePostal))
    .ForMember(g => g.nomRue, map => map.MapFrom(vm => vm.nomRue))
    .ForMember(g => g.numRue, map => map.MapFrom(vm => vm.numRue))
    .ForMember(g => g.pays, map => map.MapFrom(vm => vm.pays))
    .ForMember(g => g.ville, map => map.MapFrom(vm => vm.ville));

CreateMap<UtilisateurDTO, Utilisateur>().MaxDepth(1)
    .ForMember(g => g.id, map => map.MapFrom(vm => vm.id))
    .ForMember(g => g.dConnexion, map => map.MapFrom(vm => vm.dConnexion))
    .ForMember(g => g.dCreation, map => map.MapFrom(vm => vm.dCreation))
    .ForMember(g => g.isActive, map => map.MapFrom(vm => vm.isActive))
    .ForMember(g => g.isDeleted, map => map.MapFrom(vm => vm.isDeleted))
    .ForMember(g => g.login, map => map.MapFrom(vm => vm.login))
    .ForMember(g => g.password, map => map.MapFrom(vm => vm.password))
    .ForMember(g => g.personne, map => map.MapFrom(vm => vm.personne)).MaxDepth(1);

CreateMap<EmployeDTO, Employe>().MaxDepth(1)
    .ForMember(g => g.id, map => map.MapFrom(vm => vm.id))
    .ForMember(g => g.typeEmploye, map => map.MapFrom(vm => vm.typeEmploye))
    .ForMember(g => g.affectationServices, map => map.MapFrom(vm => vm.affectationServices))
    .ForMember(g => g.utilisateur, map => map.MapFrom(vm => vm.utilisateur)).MaxDepth(1)
    .ForMember(g => g.adresse, map => map.MapFrom(vm => vm.adresse)).MaxDepth(1);

CreateMap<TEmployeDTO, TEmploye>().MaxDepth(1)
           .ForMember(g => g.libe, map => map.MapFrom(vm => vm.libe));

CreateMap<AffectationServiceDTO, AffectationService>().MaxDepth(1)
            .ForMember(g => g.id, map => map.MapFrom(vm => vm.id))
            .ForMember(g => g.employe, map => map.MapFrom(vm => vm.employe)).MaxDepth(1)
            .ForMember(g => g.groupe, map => map.MapFrom(vm => vm.groupe)).MaxDepth(1)
            .ForMember(g => g.isPrincipal, map => map.MapFrom(vm => vm.isPrincipal))
            .ForMember(g => g.service, map => map.MapFrom(vm => vm.service)).MaxDepth(1);

流利的EF6映射

public PersonneConfiguration()
{
    ToTable("Personne");
    HasKey<int>(a => a.id);
    HasRequired<Adresse>(x => x.adresse);
    HasOptional<Utilisateur>(x => x.utilisateur);
    Property(a => a.civ).HasColumnType("varchar").HasMaxLength(3);
    Property(a => a.nom).HasColumnType("varchar").HasMaxLength(80);
    Property(a => a.prenom).HasColumnType("varchar").HasMaxLength(80);
    Property(a => a.email).HasColumnType("varchar").HasMaxLength(150);
    Property(a => a.tel1).HasColumnType("varchar").HasMaxLength(10);
    Property(a => a.tel2).HasColumnType("varchar").HasMaxLength(10);
    Property<bool>(a => a.isClient);
    Property<bool>(a => a.isFournisseur);
    Property<bool>(a => a.isDeleted);
}

public EmployeConfiguration()
{
    ToTable("Employe");
    HasKey<int>(a => a.id);
    HasOptional<TEmploye>(x => x.typeEmploye);
    Property<bool>(a => a.isDeleted);
}

public AdresseConfiguration()
{
    ToTable("Adresse");
    HasKey<int>(a => a.id);
    Property(a => a.codePostal).HasColumnType("varchar").HasMaxLength(5);
    Property(a => a.nomRue).HasColumnType("varchar").HasMaxLength(150);
    Property(a => a.numRue).HasColumnType("varchar").HasMaxLength(10);
    Property(a => a.ville).HasColumnType("varchar").HasMaxLength(80);
    Property(a => a.pays).HasColumnType("varchar").HasMaxLength(80);
}

public AffectationServiceConfiguration()
    {
        ToTable("AffectationService");
        HasKey<int>(a => a.id);
        HasRequired<Employe>(x => x.employe).WithMany(x => x.affectationServices);
        HasRequired<Service>(x => x.service);
        HasRequired<Droit>(x => x.groupe);
        Property<bool>(a => a.isPrincipal);

    }

public TEmployeConfiguration()
    {
        ToTable("TEmploye");
        HasKey<int>(a => a.id);
        Property(a => a.libe).HasColumnType("varchar").HasMaxLength(100);

    }

和ViewModel taht导致错误

 public class EditEmployeViewModel
    {
        public EmployeDTO personne { get; set; }

        public EditEmployeViewModel()
        {
            personne = new EmployeDTO();

        }
    }

当创建一个新的EmployeDTO()时,将发生堆栈溢出。 我试图通过在可能的地方添加.MaxDepth(1)来解决此问题,但是它不起作用...对不起,我知道这是一篇很长的文章,但是我对我的问题一无所知,所以我尝试向您展示我代码中最相关的部分。

谢谢大家,我是EF6的新手,如果您有任何技巧或好的建议可以分享。

谢谢您的帮助。 我没有找到真正的解决方案,所以我在对象Adresse中删除了引用Personne以避免stackoverflow异常...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM