Using Include in Entity Framework Core

asp.net-mvc entity-framework-core linq-to-entities

Question

Question: How and where the extension method "Include", used in the Index() action method, in the following PostController used in the Inxex.cshtml view shown below? As I understand _context.Posts.Include(p => p.Blog) means include all posts that relate to blogs table. But I don't see use of blog class or blogId property in the Index.cshtml view below?

Background: In an ASP.NET MVC Core - Code First project I'm following this ASP.NET official site tutorial where they have following Model classes for Blog (parent) and Post (child). I then created a controller (shown below) using MVC Controller with Views, using Entity Framework wizard where I selected Post model in the Model dialogbox.:

Model:

using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

namespace EFGetStarted.AspNetCore.NewDb.Models
{
    public class BloggingContext : DbContext
    {
        public BloggingContext(DbContextOptions<BloggingContext> options)
            : base(options)
        { }

        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }

        public List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }
}

PostController:

public class PostsController : Controller
{
        private readonly BloggingContext _context;

        public PostsController(BloggingContext context)
        {
            _context = context;    
        }

        // GET: Posts
        public async Task<IActionResult> Index()
        {
            var bloggingContext = _context.Posts.Include(p => p.Blog);
            return View(await bloggingContext.ToListAsync());
        }
}

Index.cshtml view for Index() action in PostController:

@model IEnumerable<ASP_Core_Blogs.Models.Post>

@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Content)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Title)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Content)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <td>
                <a asp-action="Edit" asp-route-id="@item.PostId">Edit</a> |
                <a asp-action="Details" asp-route-id="@item.PostId">Details</a> |
                <a asp-action="Delete" asp-route-id="@item.PostId">Delete</a>
            </td>
        </tr>
}
    </tbody>
</table>
1
0
9/10/2016 9:20:11 PM

Accepted Answer

Since you're not accessing the Blog property, it's better not to use Include(p => p.Blog). This will add an extra join that isn't required. However, if you'll reference it in each table row, then it's preferred to include it to avoid lazy loading issues.

0
9/10/2016 10:10:04 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow