https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.2
代码 :https://github.com/softuse/VSCODE
190523文件夹
If the following route template is added to the Index page, the search string can be passed as a URL segment (for example, https://localhost:5001/Movies/Ghost).
CSHTML复制
@page "{searchString?}"
public async Task OnGetAsync()
{
// Movie = await _context.Movie.ToListAsync();
var movies=from movie in _context.Movie
select movie;
//加入查询条件
// 1.先判断是否为空
// 2.查询包含字符串的条件
if(!string.IsNullOrEmpty(SearchString))
{
//The Contains method is run on the database, not in the C# code.
movies=movies.Where(movie=>movie.Title.Contains(SearchString));
}
Movie=await movies.ToListAsync();
}
https://localhost:5001/Movies/Ghost

However, you can't expect users to modify the URL to search for a movie. 在此步骤中,会添加 UI 来筛选电影。In this step, UI is added to filter movies. 如果已添加路由约束 "{searchString?}",请将它删除。If you added the route constraint "{searchString?}", remove it.
打开 Pages/Movies/Index.cshtml 文件,并添加以下代码中突出显示的 <form> 标记:Open the Pages/Movies/Index.cshtml file, and add the <form> markup highlighted in the following code:
CSHTML复制
@page
@model RazorPagesMovie.Pages.Movies.IndexModel
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-page="Create">Create New</a>
</p>
<form>
<p>
Title: <input type="text" asp-for="SearchString" />
<input type="submit" value="Filter" />
</p>
</form>
<table class="table">
@*Markup removed for brevity.*@
There are a few approaches to resolving the error:
-
让 Entity Framework 自动丢弃并使用新的模型类架构重新创建数据库。Have the Entity Framework automatically drop and re-create the database using the new model class schema. 此方法在开发周期早期很方便;通过它可以一起快速改进模型和数据库架构。This approach is convenient early in the development cycle; it allows you to quickly evolve the model and database schema together. 此方法的缺点是会导致数据库中的现有数据丢失。The downside is that you lose existing data in the database. 请勿对生产数据库使用此方法!Don't use this approach on a production database! 当架构更改时丢弃数据库并使用初始值设定项以使用测试数据自动设定数据库种子,这通常是开发应用的有效方式。Dropping the DB on schema changes and using an initializer to automatically seed the database with test data is often a productive way to develop an app.
-
对现有数据库架构进行显式修改,使它与模型类相匹配。Explicitly modify the schema of the existing database so that it matches the model classes. 此方法的优点是可以保留数据。The advantage of this approach is that you keep your data. 可以手动或通过创建数据库更改脚本进行此更改。You can make this change either manually or by creating a database change script.
-
使用 Code First 迁移更新数据库架构。Use Code First Migrations to update the database schema.
对于本教程,请使用 Code First 迁移。For this tutorial, use Code First Migrations.
更新 SeedData 类,使它提供新列的值。Update the SeedData class so that it provides a value for the new column. 示例更改如下所示,但可能需要对每个 new Movie 块做出此更改。A sample change is shown below, but you'll want to make this change for each new Movie block.
C#复制
context.Movie.AddRange(
new Movie
{
Title = "When Harry Met Sally",
ReleaseDate = DateTime.Parse("1989-2-12"),
Genre = "Romantic Comedy",
Price = 7.99M,
Rating = "R"
},
Add a migration for the rating field
从“工具”菜单中,选择“NuGet 包管理器”>“包管理器控制台”。From the Tools menu, select NuGet Package Manager > Package Manager Console. 在 PMC 中,输入以下命令:In the PMC, enter the following commands:
PowerShell复制
Add-Migration Rating
Update-Database
Add-Migration 命令会通知框架执行以下操作:The Add-Migration command tells the framework to:
- 将
Movie模型与MovieDB 架构进行比较。Compare theMoviemodel with theMovieDB schema. - 创建代码以将 DB 架构迁移到新模型。Create code to migrate the DB schema to the new model.
名称“Rating”是任意的,用于对迁移文件进行命名。The name "Rating" is arbitrary and is used to name the migration file. 为迁移文件使用有意义的名称是有帮助的。It's helpful to use a meaningful name for the migration file.
Update-Database 命令指示框架将架构更改应用到数据库。The Update-Database command tells the framework to apply the schema changes to the database.
如果删除 DB 中的所有记录,种子初始值设定项会设定 DB 种子,并将包括 Rating 字段。If you delete all the records in the DB, the initializer will seed the DB and include the Rating field. 可以使用浏览器中的删除链接,也可以从 Sql Server 对象资源管理器 (SSOX) 执行此操作。You can do this with the delete links in the browser or from Sql Server Object Explorer (SSOX).
博客围绕数据库架构更新展开,介绍了几种解决错误的方法,如让Entity Framework自动丢弃并重建数据库、显式修改现有数据库架构、使用Code First迁移更新架构等。重点讲解了使用Code First迁移的步骤,包括添加评级字段迁移、在包管理器控制台输入命令等。
1万+

被折叠的 条评论
为什么被折叠?



