Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
asadsaeed opened this issue Mar 16, 2018 · 0 comments

Comments

@asadsaeed
Copy link

asadsaeed commented Mar 16, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant