AutoMapper – Get Table Record with Foreign Table Record & Map to View Model

AutoMapper – Get Table Record with Foreign Table & Map to View Model
 
Entity Framework Initialization :
 
The Database needs to be configured Table Relationship well with Primary Key and Foreign Key.
 
The Entity Framework Table Entity would be defined the Foreign Table Property on the Model.
– For Example from TEAM Table:
( public virtual ICollection PLAYERs { get; set; } ).
 
When LINQ Query is executed on the Master Table, the returned Object would also have the Foreign Table Record on that Property based on the Table Relationship.
 
Then, the Entity Table Model can be mapped on the View Model with the relevant Table Field from the Web Application by using AutoMapper.

 
   Entity Framework – Table Entity
 
   [Table("TEAM")]
   public partial class TEAM
   {
      … …
 
      [ … … ]
      public virtual ICollection PLAYERs { get; set; }
   }
 
 
   ASP.NET MVC – Web Application View Model
 
   public class TEAM
   {
      … …
 
      public List<PLAYER> PLAYER { get; set; }
   }
 
 
   AutoMapper Configuration
 
   this.CreateMap<SoccerDAO.TEAM, WebApp.Models.TEAM>()
      .ForMember( d => d.PLAYER, e => e.MapFrom(s => s.PLAYERs) );