2

I'm not sure where to start doing this with entity framework. Here are samples of the table data. The tables are created via code first.

I have 5 tables:  
Id - Person  
12 - John  
13 - Sarah  
14 - Bob  
15 - Frank  

Id - Package  
1 - Standard  
2 - Extra  
3 - Gold  
4 - Off Season  

Id - Features  
21 - Feature A  
22 - Feature B  
23 - Feature C  
24 - Feature D  

Link Tables  
PersonId - PackageId  
12 - 1  
12 - 4  
13 - 3  
14 - 2  
15 - 1  
15 - 2  

PackageId - FeatureId  
1 - 21  
1 - 22  
2 - 21  
2 - 22  
2 - 23  
3 - 21  

I know that I can find out a Person's Features like this:

var person = _db.Persons
    .Where(p => p.Id == id)
    .Include(p => p.Packages)
    .Include("Packages.Features")
    .SingleOrDefault();

However I would like to get a ViewModel class or even a list that has only these columns:

PersonId Person FeatureId Feature
12 - John - 21 - Feature A
12 - John - 22 - Feature B

I know I can do it with SQL. I'm just not sure how to do it with Linq and Entity Framework. What's the best practice to do that?

2
  • You really have to show some first efforts, otherwise we ave no idea where to begin. I mean, can we assume you know how to use EF basically? And basic knowledge of LINQ? Commented Jul 14, 2015 at 19:48
  • Are you using a data access layer? Code first or DB first? You can fill your collections in either the controller or the view model. Before we can help you you have to show us what you are doing first. Commented Jul 14, 2015 at 20:08

1 Answer 1

2

I would recommend using EF Code First. You can create the following classes:

public class Person
{   
    [Key]
    public int Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    ...

    public ICollection<Package> Packages { get; set; }
}

public class Package
{   
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    ...

    public ICollection<Feature> Features { get; set; }
}

public class Feature
{   
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    ...

}

Then you could create a ViewModel like this:

public class PersonViewModel
{
    public int Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    ...

    public List<FeatureViewModel> Features { get; set; }
}

You can get the data using LINQ To Entities like this:

var person = _db.Persons
                .Where(p => p.Id == id)
                .Include(p => p.Packages)
                .Include("Packages.Features")
                .SingleOrDefault();

And to fill the ViewModel using Automapper for example:

var personViewModel = Mapper.Map<PersonViewModel>(person);
Sign up to request clarification or add additional context in comments.

1 Comment

That last part, the Automapper, was really the only thing I was looking for! Thank you! One question... FeatureViewModel, is that another ViewModel? or did you mean List<Feature>?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.