aspnet core_基本增删改查

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:

  1. 让 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.

  2. 对现有数据库架构进行显式修改,使它与模型类相匹配。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.

  3. 使用 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 模型与 Movie DB 架构进行比较。Compare the Movie model with the Movie DB 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).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值