locked
Issue with Automapper RRS feed

  • Question

  • User-1617232794 posted

    I am facing some issue with AutoMapper. I don't know how to resolve it. please help me. below is my code:

    I have two entity class. This is same as DBContext class of DBML.

    public class UserEntity
    {
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public int UserTypeId { get; set; }
    public UserTypeEntity UserType { get; set; }
    }
    
    public class UserTypeEntity : BaseEntity
    {
    public int? Id { get; set; }
    public System.Nullable<byte> UserLevel { get; set; } 
    public string Description { get; set; }
    }

    Now I am setting value of UserEntity like:

    UserEntity entity = new UserEntity()
    entity.FirstName = "Ajay";
    entity.MiddleName = "K";
    entity.LastName = "Patel";
    entity.UserTypeId = 3;

    So entity.UserType is null. Now I am mapping UserEntity with UserMaster(DBML generated class) like this :

    Mapper.CreateMap<UserEntity, UserMaster>();
    var master = Mapper.Map<UserEntity, UserMaster>(entity);

    now when I checked result the value of master.UserTypeID is getting null as it is exists in entity bit not mapped.


    UserTypeID is the FK in UserMaster to UserType Table

    Monday, January 12, 2015 6:01 AM

All replies

  • User-1990124160 posted

    I am facing some issue with AutoMapper. I don't know how to resolve it. please help me. below is my code:

    I have two entity class. This is same as DBContext class of DBML.

    public class UserEntity
    {
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public int UserTypeId { get; set; }
    public UserTypeEntity UserType { get; set; }
    }
    
    public class UserTypeEntity : BaseEntity
    {
    public int? Id { get; set; }
    public System.Nullable<byte> UserLevel { get; set; } 
    public string Description { get; set; }
    }

    Now I am setting value of UserEntity like:

    UserEntity entity = new UserEntity()
    entity.FirstName = "Ajay";
    entity.MiddleName = "K";
    entity.LastName = "Patel";
    entity.UserTypeId = 3;

    So entity.UserType is null. Now I am mapping UserEntity with UserMaster(DBML generated class) like this :

    Mapper.CreateMap<UserEntity, UserMaster>();
    var master = Mapper.Map<UserEntity, UserMaster>(entity);

    now when I checked result the value of master.UserTypeID is getting null as it is exists in entity bit not mapped.


    UserTypeID is the FK in UserMaster to UserType Table

    You should show the UserMaster type since that is the destination type to be converted into in this AutoMapper mapping. Otherwise, it is difficult to tell.
    But my best guess is that there is no UserTypeId property in UserMaster table or their names do not match. Thus, it is not being set.

    Monday, January 12, 2015 7:57 AM
  • User-1617232794 posted

    Thanks for reply.

    Y

    But my best guess is that there is no UserTypeId property in UserMaster table or their names do not match. Thus, it is not being set.

    There is a column with UserTypeID in Table and also same property in entity class. Still I can't able to map

    Friday, January 16, 2015 6:35 AM
  • User-1990124160 posted

    ajay.kalol

    There is a column with UserTypeID in Table and also same property in entity class. Still I can't able to map

    You already posted your UserEntity POCO just do the same for UserMaster POCO and I or someone else can post a working executable solution showing how to map it successfully. That would be the best option for you to get a solution quickly.

    Friday, January 16, 2015 6:47 AM
  • User-1617232794 posted

    UserMaster POCO is same as UserEntity but it is DBML generated class. So you can proceed with same

    Tuesday, January 20, 2015 2:26 AM
  • User-1990124160 posted

    UserMaster POCO is same as UserEntity but it is DBML generated class. So you can proceed with same

    With your provided answer I have tested out a sample app and I do not see any problem in mapping.

    https://dotnetfiddle.net/ut84yu 

    Tuesday, January 20, 2015 4:19 AM
  • User-1617232794 posted

    Let me test it again then I will reply back.

    Thursday, January 22, 2015 1:22 AM