1

I've been working on this architecture for more than 8 hours. First time with WCF and AutoMapper. Still no solution.

I've read every question about the subject but couldn't implement any of the answers for my project. So, if you find this a duplicate one please refer.

I have a database of 83 tables. Many of them are related to each other in some way and the most related one is called Sicil. Let's work with just one related table called Access. I need to create the mappings so whenever I call Sicil, it should call Access object and vice versa (without StackOverFlow Exception as spoken at AutoMapper throwing StackOverflowException when calling ProjectTo<T>() on IQueryable).

Here is the Sicil entity:

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...
}

And here is my Access entity:

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; }
}

The DTOs I've created for data transfers:

Access:

[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();
    }
}

Sicil:

[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>();
    }
}

I've edited the mappings so much that I'm not sure which one should work now:

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)); 

And finally my caller method:

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

With this configuration, I take the following exception:

Error mapping types.

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

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

Property:
Sicil

Without related collections or objects, I get data with no errors however the project should work as I mentioned. Hope we can find a solution.

Doğa Gençer
  • 124
  • 3
  • 15

0 Answers0