Have you looked at the Include method?

Thanks
Clint Colefax

From: ozdotnet-boun...@ozdotnet.com [mailto:ozdotnet-boun...@ozdotnet.com] On 
Behalf Of anthonyatsmall...@mail.com
Sent: Thursday, 8 May 2014 12:30 AM
To: ozdotnet@ozdotnet.com
Subject: MVC Model populating from EF5

Trying to populate a viewmodel from ef ..i can populate the model from ef with 
no issue but how do I populate the related objects?  I'm sure it easy solution!


IssueTracker_Issues is the database table from EF

IssueTracker_IssuesTypes is the database table related to field IssueTypeId 
from IssueTracker_Issues


namespace SIMIt.Models
{
    public class IssueViewModel
    {


        public IEnumerable<IssueViewModel> GetIssues()
        {
            var db = new SBHUBDBContext();

              //This populates IssueViewModel but want to also populate the 
related model\tables
            IEnumerable<IssueViewModel> albums = 
db.IssueTracker_Issues.Take(5).ToList().Select(a => new IssueViewModel
                  {
                      IssueId = a.IssueId,
                      IssueTitle = a.IssueTitle,
                      IssueStatusId = a.IssueStatusId,
                      IssueTypeId = a.IssueTypeId,
                      IssueMemberID = a.IssueStatusId,
                      IssueComments = a.IssueComments,
                      IssuePriorityId = a.IssuePriorityId
                  });

            return albums;
        }


        public IssueViewModel()
        {
        }

        public int IssueId { get; set; }
        public string IssueTitle { get; set; }
        public int IssueCategoryId { get; set; }
        public int IssuePriorityId { get; set; }
        public int IssueStatusId { get; set; }
        public int IssueCreatorId { get; set; }
        public int IssueOwnerId { get; set; }
        public IssueTypeViewModel IssueType { get; set; }  //relation
    }
}



    public class IssueTypeViewModel
    {
        public int IssueTypeId { get; set; }
        public string IssueType { get; set; }
    }




Anthony Salerno | Founder | SmallBiz Australia
Innovation | Web | Software | Developers | Support
Phone  : +613 8400 4191 Email  : 2Anthony (at) smallbiz.com.au   Postal : Po 
Box 135, Lower Plenty 3093 ABN : 16 079 706 737
Website : www.smallbiz.com.au<http://www.smallbiz.com.au/>


Reply via email to