Skip to content

Circular referencing leading to stackoverflow exception while converting my dbcontext object into DTO object #151

@asadsaeed

Description

@asadsaeed

I am using entity framework and i have created my DTO objects to transfer data to client. I am using Express mapper to convert from context objects to DTO objects and vise versa. Now that my context objects contain circular referencing and while converting it to my DTO object it leads to stackoverflow exception.

Prior to this i was using Auto Mapper to serve the purpose and i had to use MaxDepth(1) configuration to get rid of this circular referencing but had to cut off using Auto mapper because of performance issue. Now that using Express mapper i can't find out how to solve this problem

below is my context class for user entity

    public partial class user
     {
        public user()
        {
        this.layouts = new HashSet<layout>();  
       }
       public int UserId { get; set; }
       public string UserName { get; set; }
       public virtual ICollection<layout> layouts { get; set; }
     }

And below is my DTO class for user entity

  public class UserEntity
   {
    public int UserId { get; set; }
    public string UserName { get; set; }
    public List<LayoutEntity> layouts { get; set; }
   }

Here is how i am trying to map the user into UserEntity

      public class UserDAL : DALs
     {
        public UserDAL (string Client) : base(Client)
     {
        Mapper.Register<user, UserEntity>();
     }
     public List<UserEntity> GetAllUsers()
     {
         try
         {
             List<UserEntity> UserEntityList = new List<UserEntity>();
             List<user> UsersList = db.users.ToList();
            
            // i have tried this 
             //UserEntityList = UsersList .Select(x => x.Map<user, UserEntity>()).ToList();
            / / and this
              UsersList .Map<List<user>,List<UserEntity>>();
            // but it leads to stackoverflow exception 
             return UserEntityList;
           }
            catch (Exception ex)
            {
             Log4Net.WriteException(ex);
             return null;
            }
         }
     }

Please Help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions