在 Entity Framework 核心中使用包含
Using Include in Entity Framework Core
问题:扩展方法“Include”在Index()
操作方法中的使用方式和位置,在下面PostController
在下面显示的 Inxex.cshtml view
中使用?据我了解 _context.Posts.Include(p => p.Blog)
表示包括与博客相关的所有帖子 table。但是我在下面的 Index.cshtml 视图中没有看到博客 class or blogId
属性 的使用?
背景:在一个 ASP.NET MVC Core - Code First 项目中,我正在关注 this ASP.NET official site tutorial 他们有以下模型 classes对于 Blog
(parent) 和 Post
(child)。然后,我使用 MVC Controller with Views, using Entity Framework
向导创建了一个控制器(如下所示),我在“模型”对话框中选择了 Post
模型。:
型号:
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; }
}
}
后控制器:
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 在 PostController 中查看 Index() 操作:
@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>
由于您不访问博客 属性,最好不要使用 Include(p => p.Blog)。这将添加一个不需要的额外连接。
但是,如果您将在每个 table 行中引用它,那么最好包含它以避免延迟加载问题。
问题:扩展方法“Include”在Index()
操作方法中的使用方式和位置,在下面PostController
在下面显示的 Inxex.cshtml view
中使用?据我了解 _context.Posts.Include(p => p.Blog)
表示包括与博客相关的所有帖子 table。但是我在下面的 Index.cshtml 视图中没有看到博客 class or blogId
属性 的使用?
背景:在一个 ASP.NET MVC Core - Code First 项目中,我正在关注 this ASP.NET official site tutorial 他们有以下模型 classes对于 Blog
(parent) 和 Post
(child)。然后,我使用 MVC Controller with Views, using Entity Framework
向导创建了一个控制器(如下所示),我在“模型”对话框中选择了 Post
模型。:
型号:
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; }
}
}
后控制器:
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 在 PostController 中查看 Index() 操作:
@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>
由于您不访问博客 属性,最好不要使用 Include(p => p.Blog)。这将添加一个不需要的额外连接。 但是,如果您将在每个 table 行中引用它,那么最好包含它以避免延迟加载问题。