针对WCF的AutoMapper多对多关系

时间:2017-03-14 14:27:56

标签: c# entity-framework wcf mapping automapper

我已经在这个架构上工作了8个多小时。第一次使用WCF和AutoMapper。仍然没有解决方案。

我已经阅读了有关该主题的所有问题,但无法为我的项目实施任何答案。因此,如果您发现这是重复的,请参阅。

我有一个包含83个表的数据库。他们中的许多人在某种程度上彼此相关,最相关的一个叫做西西里。让我们只使用一个名为Access的相关表。我需要创建映射,所以每当我调用Sicil时,它应该调用Access对象,反之亦然(没有像AutoMapper throwing StackOverflowException when calling ProjectTo<T>() on IQueryable所说的StackOverFlow Exception)。

这是Sicil实体:

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

    public int Id { get; set; }
    public string Isim { get; set; }
    public string Soyad { get; set; }
    public int Firma { get; set; }
    public int Departmant { get; set; }
    public int Pozisyon { get; set; }
    public string Tc { get; set; }
    public string SGKNo { get; set; }
    public System.DateTime GirisTarihi { get; set; }
    public Nullable<System.DateTime> CikisTarihi { get; set; }
    public string SicilNo { get; set; }
    public string KanGrubu { get; set; }
    public string MobilTel { get; set; }
    public string EvTRel { get; set; }
    public string Tel2 { get; set; }
    public int Il { get; set; }
    public int Ilce { get; set; }
    public string Adres { get; set; }
    public bool Cinsiyet { get; set; }
    public string EMail { get; set; }
    public Nullable<System.DateTime> MuayneTarihi { get; set; }
    public Nullable<System.DateTime> SahaGirisTarihi { get; set; }
    public int MinorToplam { get; set; }
    public int MajorToplam { get; set; }
    public Nullable<System.DateTime> DogumTarihi { get; set; }
    public string Gorev { get; set; }
    public bool Aktif { get; set; }
    public byte[] Fotograf { get; set; }
    public string KartNo { get; set; }
    public string Sifre { get; set; }
    public string Yaka { get; set; }
    public string OKod1 { get; set; }
    public string OKod2 { get; set; }
    public string OKod3 { get; set; }
    public string OKod4 { get; set; }
    public string OKod5 { get; set; }
    public string OKod6 { get; set; }
    public string OKod7 { get; set; }
    public string OKod8 { get; set; }
    public string OKod9 { get; set; }
    public string OKod10 { get; set; }
    public string AyrilisNedeni { get; set; }
    public Nullable<bool> OFM { get; set; }
    public Nullable<System.DateTime> LastEditDate { get; set; }
    public Nullable<System.DateTime> CreationDate { get; set; }

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

这是我的Access实体:

public partial class Access
{
    public int Id { get; set; }
    public System.DateTime IslemTarihi { get; set; }
    public int CihazId { get; set; }
    public int SicilId { get; set; }
    public bool HesaplamaYapildi { get; set; }
    public int KayitTipi { get; set; }
    public bool Durumu { get; set; }
    public int Tipi { get; set; }
    public Nullable<System.DateTime> LastEditDate { get; set; }
    public Nullable<System.DateTime> CreationDate { get; set; }

    public virtual Sicil Sicil { get; set; }
}

我为数据传输创建的DTO:

访问:

[DataContract]
public partial class Access
{
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public System.DateTime IslemTarihi { get; set; }
    [DataMember]
    public int CihazId { get; set; }
    [DataMember]
    public int SicilId { get; set; }
    [DataMember]
    public bool HesaplamaYapildi { get; set; }
    [DataMember]
    public int KayitTipi { get; set; }
    [DataMember]
    public bool Durumu { get; set; }
    [DataMember]
    public int Tipi { get; set; }

    public Access()
    {
        Sicil = new SicilDTO();
    }

    [DataMember]
    public virtual SicilDTO Sicil { get; set; }
}

AccessDTO:

[DataContract]
public partial class AccessDTO : Access
{
    [DataMember]
    public virtual SicilDTO SicilDto { get; set; }
    public AccessDTO()
    {
        SicilDto = new SicilDTO();
    }
}

西西里:

[DataContract]
public partial class Sicil
{
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string Isim { get; set; }
    [DataMember]
    public string Soyad { get; set; }
    [DataMember]
    public int Firma { get; set; }
    [DataMember]
    public int Departmant { get; set; }
    [DataMember]
    public int Pozisyon { get; set; }
    [DataMember]
    public string Tc { get; set; }
    [DataMember]
    public string SGKNo { get; set; }
    [DataMember]
    public System.DateTime GirisTarihi { get; set; }
    [DataMember]
    public Nullable<System.DateTime> CikisTarihi { get; set; }
    [DataMember]
    public string SicilNo { get; set; }
    [DataMember]
    public string KanGrubu { get; set; }
    [DataMember]
    public string MobilTel { get; set; }
    [DataMember]
    public string EvTRel { get; set; }
    [DataMember]
    public string Tel2 { get; set; }
    [DataMember]
    public int Il { get; set; }
    [DataMember]
    public int Ilce { get; set; }
    [DataMember]
    public string Adres { get; set; }
    [DataMember]
    public bool Cinsiyet { get; set; }
    [DataMember]
    public string EMail { get; set; }
    [DataMember]
    public Nullable<System.DateTime> MuayneTarihi { get; set; }
    [DataMember]
    public Nullable<System.DateTime> SahaGirisTarihi { get; set; }
    [DataMember]
    public int MinorToplam { get; set; }
    [DataMember]
    public int MajorToplam { get; set; }
    [DataMember]
    public Nullable<System.DateTime> DogumTarihi { get; set; }
    [DataMember]
    public string Gorev { get; set; }
    [DataMember]
    public bool Aktif { get; set; }
    [DataMember]
    public byte[] Fotograf { get; set; }
    [DataMember]
    public string KartNo { get; set; }
    [DataMember]
    public string Sifre { get; set; }
    [DataMember]
    public string Yaka { get; set; }
    [DataMember]
    public string OKod1 { get; set; }
    [DataMember]
    public string OKod2 { get; set; }
    [DataMember]
    public string OKod3 { get; set; }
    [DataMember]
    public string OKod4 { get; set; }
    [DataMember]
    public string OKod5 { get; set; }
    [DataMember]
    public string OKod6 { get; set; }
    [DataMember]
    public string OKod7 { get; set; }
    [DataMember]
    public string OKod8 { get; set; }
    [DataMember]
    public string OKod9 { get; set; }
    [DataMember]
    public string OKod10 { get; set; }
    [DataMember]
    public string AyrilisNedeni { get; set; }
    [DataMember]
    public Nullable<bool> OFM { get; set; }

    public Sicil()
    {
        Access = new HashSet<AccessDTO>();
    }

    [DataMember]
    public virtual ICollection<AccessDTO> Access { get; set; }        
}

SicilDTO:

[DataContract]
public partial class SicilDTO : Sicil
{
    [DataMember]
    public virtual ICollection<AccessDTO> Accessler { get; set; }
    public SicilDTO()
    {
        Accessler = new HashSet<AccessDTO>();
    }
}

我已经编辑了很多映射,以至于我不确定哪一个应该可以正常工作:

cfg.CreateMap<Data.Access, Access>().ForMember(dest => dest.Sicil, opt => opt.MapFrom(src => src.Sicil)).PreserveReferences();

cfg.CreateMap<Access, Data.Access>().ForMember(dest => dest.LastEditDate, opt => opt.Ignore()).ForMember(dest => dest.CreationDate, opt => opt.Ignore());

cfg.CreateMap<Sicil, Data.Sicil>().ForMember(dest => dest.LastEditDate, opt => opt.Ignore()).ForMember(dest => dest.CreationDate, opt => opt.Ignore()).ForMember(dest => dest.Access, opt => opt.MapFrom(src => src.Access));

cfg.CreateMap<Data.Sicil, Sicil>().ForMember(dest => dest.Access, opt => opt.MapFrom(src => src.Access)); 

最后我的来电方法:

public Access IddenAccessGetir(int id)
    {
        var access = _dataServis.IddenAccessGetir(id);        
        var model = Mapper.Map<Access>(access); //throws exception
        return model;
    }

使用此配置,我采用以下异常:

Error mapping types.

Mapping types:
Access -> Access
Data.Access -> DinibhDataService.Access

Type Map configuration:
Access -> Access
Data.Access -> DinibhDataService.Access

Property:
Sicil

没有相关的集合或对象,我得到的数据没有错误,但项目应该像我提到的那样工作。希望我们能找到解决方案。

0 个答案:

没有答案