-4

I have a collection property of DTO like this

 public ICollection<Applicant> Applicants{get;set;}

Applicant Model

public class Applicant
{
   public int Id{get;set;}
   public string name{get;set;}
   public ICollection<ApplicantSkillsVM> ApplicantSkills { get; set; }
}

public class ApplicantSkillsVM
{
   public int Id {get;set;}
   public Skill skill{get;set;}
}

I want to map my List<iApplicant> DTO to entity given that I want to take ApplicantSkillsVM but ignore skill inside ApplicantSkillsVM.

I have a model which is list List<Applicant> and that contains another list List<ApplicantSkillsVM> and ApplicantSkillsVM has a property skill. I want to ignore this (skill) while mapping. Its simple.

How can I do this in latest the AutoMapper version with EF6?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
user786
  • 3,902
  • 4
  • 40
  • 72
  • Sorry but this line is very confusing: "I want to Map my List DTO to entity given that I want to take ApplicantSkillsVM but ignore skill inside ApplicantSkillsVM". Can you please go a bit more in depth about what you are trying to achieve? Why is there special accent on Map (capital M)? What is iApplicant? – Igor Mar 25 '18 at 10:27
  • your question isn't clarify, please review it. – Soheil Alizadeh Mar 25 '18 at 10:28
  • @Igor Its simple. I have a `model` which is list `List` and that contains another list `List` and `ApplicantSkillsVM` has a property `skill`. I want to ignore this(skill) while mapping. Its simple. Are you aware of automapper and EF? – user786 Mar 25 '18 at 10:34
  • Something like this? https://stackoverflow.com/questions/4987872/ignore-mapping-one-property-with-automapper – gcores Mar 25 '18 at 11:17
  • @gcores please read my scenario and then let me know, its simple but not that simple – user786 Mar 26 '18 at 05:34
  • @Alex. Stange I have answerd almost the same question here last week. I give you the same answer again. This time please give a clear answer what is not corret – Stephu Mar 26 '18 at 06:04

2 Answers2

1

Here a running sample:

 internal class Program
    {
        #region Methods

        private static void Main(string[] args)
        {
            // Configure the mappings
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<ApplicantSkillVM, ApplicantSkill>().ForMember(x => x.Skill, x => x.Ignore()).ReverseMap();
                cfg.CreateMap<ApplicantVM, Applicant>().ReverseMap();
            });


            var config = new MapperConfiguration(cfg => cfg.CreateMissingTypeMaps = true);
            var mapper = config.CreateMapper();

            ApplicantVM ap = new ApplicantVM
            {
                Name = "its me",
                ApplicantSkills = new List<ApplicantSkillVM>
                {
                    new ApplicantSkillVM {SomeInt = 10, SomeString = "test", Skill = new Skill {SomeInt = 20}},
                    new ApplicantSkillVM {SomeInt = 10, SomeString = "test"}
                }
            };

            List<ApplicantVM> applicantVms = new List<ApplicantVM> {ap};
            // Map
            List<Applicant> apcants = Mapper.Map<List<ApplicantVM>, List<Applicant>>(applicantVms);
        }

        #endregion
    }


    /// Your source classes
    public class Applicant
    {
        #region Properties

        public List<ApplicantSkill> ApplicantSkills { get; set; }

        public string Name { get; set; }

        #endregion
    }

    public class ApplicantSkill
    {
        #region Properties

        public Skill Skill { get; set; }

        public int SomeInt { get; set; }
        public string SomeString { get; set; }

        #endregion
    }

    // Your VM classes
    public class ApplicantVM
    {
        #region Properties

        public List<ApplicantSkillVM> ApplicantSkills { get; set; }

        public string Description { get; set; }
        public string Name { get; set; }

        #endregion
    }


    public class ApplicantSkillVM
    {
        #region Properties

        public Skill Skill { get; set; }

        public int SomeInt { get; set; }
        public string SomeString { get; set; }

        #endregion
    }

    public class Skill
    {
        #region Properties

        public int SomeInt { get; set; }

        #endregion
    }
}
Stephu
  • 3,236
  • 4
  • 21
  • 33
  • Error:The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Applicant__Skill__49C3F6B7". The conflict occurred in database "JOBSSITE_db", table "dbo.skills", column 'id'. The statement has been terminated – user786 Mar 26 '18 at 06:54
  • @Alex. I tested only the mapping. This is a error because of db constraint and has nothing todowith mapping – Stephu Mar 26 '18 at 06:55
  • maybe you should ask why this erros occurs. If you execute my code you see that the asked mapping behaviour works... – Stephu Mar 26 '18 at 06:58
  • still its not working same error was throwing.and down voting not gonna help you or me – user786 Mar 27 '18 at 05:49
  • I wrote you multiple times that this is not a mapping problem. Open new question with the error! – Stephu Mar 27 '18 at 05:53
-1

Initially my model ApplicantSkillsVM didnt have reference Id for Skill which should be nullable

So my model had to look like

  public class ApplicantSkillsVM{
  public int Id {get;set;}
  public int? skillId{get;set;} //updated property
  public Skill skill{get;set;}
 }

The problem resolved

user786
  • 3,902
  • 4
  • 40
  • 72
  • This has nothing todo with automapper – Stephu Mar 26 '18 at 08:59
  • @Tester row was generating in `Skill` table when it shouldnt. You couldnt figure out. I was missing nullable `Id`, it was a small problem. But you didnt find it. – user786 Mar 26 '18 at 09:32
  • Your question has the title "Ignore Mapping of inner list property in AutoMapper --EF6,C#" so your answer solves your problem on your machine, but not the asked question about how to ignore mapping! – Stephu Mar 26 '18 at 09:36