1

I have three tables:

tblApplications
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](250) NOT NULL,
    [Level5ID] [int] NOT NULL

tblLevel5s
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Level4ID] [int] NOT NULL,
    [Name] [varchar](250) NOT NULL

tblLevel4s
   [ID] [int] IDENTITY(1,1) NOT NULL,
   [Name] [varchar](250) NOT NULL

Relations between tables are: tblApplications >=o tblLevel5s >=o tblLevel4s (application can have one Level5 and Level5 can have one Level4)

I am using Entity Framework. It generated classes:

public partial class tblApplication    
    public int ID { get; set; }
    public string Name { get; set; }
    public int Level5ID { get; set; }

    public virtual tblLevel5s tblLevel5s { get; set; }
    public virtual ICollection<tblSystemApplication> tblSystemApplications { get; set; }
}

public partial class tblSystemApplication
{
    public int ID { get; set; }
    public int ApplicationID { get; set; }
    public int SystemID { get; set; }

    public virtual tblApplication tblApplication { get; set; }
    public virtual tblSystem tblSystem { get; set; }
}

public partial class tblSystem
{    
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<tblSystemApplication> tblSystemApplications { get; set; }
}

My goal is to, using automapper, create one class which will look like:

public class ApplicationDto
    {
        public int Id { get; set; }
        public SystemDto System { get; set; }
        public string Name { get; set; }
        public Level5Dto Level5 { get; set; }
        public Level4Dto Level4 { get; set; }

        public IEnumerable<SystemAppliationDto> SystemApplications { get; set; }
    }

I am not sure if I properly designed my ApplicationDto class. Please advise if anything should be changed.

What I have so far is:

cfg.CreateMap<tblApplications, ApplicationDto>()
.ForMember(dest => dest.Level5, opt => opt.MapFrom(src => src.tblLevel5s))

Now I need to add Level4 table to the mapping. Can you please help?

-- EDIT 1

What in case I have many to many relationship? I have middle table like

tblApplications
        [ID] [int] IDENTITY(1,1) NOT NULL,
        [Name] [varchar](250) NOT NULL

tblSystems
       [ID] [int] IDENTITY(1,1) NOT NULL,
       [Name] [varchar](250) NOT NULL

tblSystemApplications
       [ID] [int] IDENTITY(1,1) NOT NULL,
       [ApplicationID] [int] NOT NULL,
       [SystemID] [int] NOT NULL

Relation is tblApplications o=< tblSystemApplications >=o tblSystems What I would like to get System in ApplicationDto view.

-- EDIT 2

Added EF generated classes and updated ApplicationDto class

ironcurtain
  • 650
  • 9
  • 35
  • https://stackoverflow.com/questions/21413273/automapper-convert-from-multiple-sources – CodeCaster Sep 13 '17 at 10:10
  • In case of many to many you have a list of system in your application, if you're using EF in your tblAplications entity you have a list of tblkSystems. – Søren Sep 13 '17 at 15:58
  • @Soren Can you please clarify what you mean? I can share more details if you need. – ironcurtain Sep 13 '17 at 18:23
  • @ironcurtain you probably have list of system in your application, I'm alright? So, please edit your dto and I'll try to change the answer if is possible ;) – Søren Sep 13 '17 at 18:58
  • @Soren Updated my original post. I read somewhere that having many to many relationship is not good idea. What is your opinion about this? Please let me know if you need anything else which will help you solve my problem. Thanks in advance. – ironcurtain Sep 13 '17 at 19:38
  • @ironcurtain hey, there is no problem with many to many relationship at all, but your implementation is not correct because of that you didn't get the list of system in your application entity, I'm on vacation now and don't have computer (writing by mobile), I'll post answer next Suturday ;) – Søren Sep 14 '17 at 08:02
  • @Soren. I will wait for your answer. Enjoy your vacation:) – ironcurtain Sep 14 '17 at 08:15

1 Answers1

3

If your (generated database) Models look like this:

public class tblApplications
{
    public int Id { get; set; }
    public string Name { get; set; }
    public tblLevel5s tblLevel5 { get; set; }

}
public class tblLevel5s
{
    public int Id { get; set; }
    public string Name { get; set; }
    public tblLevel4s tblLevel4 { get; set; }
}
public class tblLevel4s
{
    public int Id { get; set; }
    public string Name { get; set; }
}

then this kind of mapping is valid:

var config = new MapperConfiguration(cfg => {
    cfg.CreateMap<tblApplications, ApplicationDto>()
    .ForMember(dest => dest.L5, opt => opt.MapFrom(src => src.tblLevel5))
    .ForMember(dest => dest.L4, opt => opt.MapFrom(src => src.tblLevel5.tblLevel4));                
});

Please let me know if your models are different.


-- EDIT 1
Your many-to-many case should be implemented something like this:

public partial class tblApplication    
    public int ID { get; set; }
    public string Name { get; set; }
    public int Level5ID { get; set; }

    public virtual tblLevel5s tblLevel5s { get; set; }
    public virtual ICollection<tblSystem> tblSystems { get; set; }
}

public partial class tblSystemApplication
{
    public int ApplicationID { get; set; }
    public int SystemID { get; set; }

    public virtual tblApplication tblApplication { get; set; }
    public virtual tblSystem tblSystem { get; set; }
}

public partial class tblSystem
{    
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<tblApplication> tblApplications { get; set; }
}

Consider that in tblSystemApplication you have Composite/Compound Primary Key (ApplicationID , SystemID)

and you'll get a list of tblSystem in your tblApplication and you can map it to whatever you want.

Søren
  • 6,517
  • 6
  • 43
  • 47
  • Thank you. This is what I was looking for. I knew it is simple but I couldn't solve "MapFrom(src => src.tblInventoryLevel5s.tblInventoryLevel4s)" part by myself. – ironcurtain Sep 13 '17 at 12:24