0

我已经阅读了很多帖子,其中一些对我有帮助,但是我仍然有一个关于使用 Entity framework 6 fluent 定义的一对一关系(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 fluent 进行映射

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 会导致错误

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

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

        }
    }

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

谢谢大家,我是 EF6 的新手,所以如果你有任何提示或好的做法可以分享,我会很高兴。

4

1 回答 1

0

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

于 2017-03-11T07:38:37.377 回答