添加数据模型类

右键单击 models 文件夹,然后单击“添加” > “类”。 将类命名“movie”。
向 movie 类添加以下属性:

using system;
using system.componentmodel.dataannotations;

namespace mvcmovie.models
{
public class movie
{
public int id { get; set; }
public string title { get; set; }

[datatype(datatype.date)]
public datetime releasedate { get; set; }
public string genre { get; set; }
public decimal price { get; set; }
}
}

 

movie 类包含:

数据库需要 id 字段以获取主键。
[datatype(datatype.date)]:datatype 属性指定数据的类型 (date)。 通过此特性:
用户无需在数据字段中输入时间信息。
仅显示日期,而非时间信息。

搭建“电影”模型的基架

基架工具将生成页面,用于对“电影”模型执行创建、读取、更新和删除 (crud) 操作。
在解决方案资源管理器中,右键单击“controllers”文件夹 >“添加”>“新搭建基架的项目”。
在“添加基架”对话框中,选择“包含视图的 mvc 控制器(使用 entity framework)”>“添加”。
填写“添加控制器”对话框:

模型类:movie (mvcmovie.models)
数据上下文类:选择 + 图标并添加默认的 mvcmovie.models.mvcmoviecontext
视图:将每个选项保持为默认选中状态
控制器名称:保留默认的 moviescontroller
选择“添加”

visual studio 将创建:
entity framework core 数据库上下文类 (data/mvcmoviecontext.cs)
电影控制器 (controllers/moviescontroller.cs)
“创建”、“删除”、“详细信息”、“编辑”和“索引”页面的 razor 视图文件 (views/movies/*.cshtml)
自动创建数据库上下文和 crud(创建、读取、更新和删除)操作方法和视图的过程称为“搭建基架”。

 

初始迁移

添加初始迁移。
使用初始迁移来更新数据库。
1.从“工具”菜单中,选择“nuget 包管理器” > “包管理器控制台”(pmc)。
2.在 pmc 中,输入以下命令:

add-migration initial
update-database

 

add-migration 命令生成用于创建初始数据库架构的代码。
数据库架构基于在 mvcmoviecontext 类中(位于 data/mvcmoviecontext.cs 文件中)中指定的模型。 initial 参数是迁移名称。
可以使用任何名称,但是按照惯例,会使用可说明迁移的名称。 有关更多信息,请参见教程:使用迁移功能 – asp.net mvc 和 ef core。
update-database 命令在用于创建数据库的 migrations/{time-stamp}_initialcreate.cs 文件中运行 up 方法。

 

检查通过依赖关系注入注册的上下文

asp.net core 通过依赖关系注入 (di) 生成。 服务(例如 ef core 数据库上下文)在应用程序启动期间通过 di 注册。
需要这些服务(如 razor 页面)的组件通过构造函数提供相应服务。

基架工具自动创建数据库上下文并将其注册到 di 容器。
我们来研究以下 startup.configureservices 方法。 基架添加了突出显示的行:

public void configureservices(iservicecollection services)
{
services.configure<cookiepolicyoptions>(options =>
{
// this lambda determines whether user consent for non-essential cookies
// is needed for a given request.
options.checkconsentneeded = context => true;
options.minimumsamesitepolicy = samesitemode.none;
});

services.addmvc().setcompatibilityversion(compatibilityversion.version_2_2);

services.adddbcontext<mvcmoviecontext>(options =>
options.usesqlserver(configuration.getconnectionstring(“mvcmoviecontext”)));\\
}

mvcmoviecontext 为 movie 模型协调 ef core 功能(创建、读取、更新、删除等)。 数据上下文 (mvcmoviecontext)
派生自 microsoft.entityframeworkcore.dbcontext。 数据上下文指定数据模型中包含哪些实体:

using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;
using microsoft.entityframeworkcore;

namespace mvcmovie.models
{
public class mvcmoviecontext : dbcontext
{
public mvcmoviecontext (dbcontextoptions<mvcmoviecontext> options)
: base(options)
{
}

public dbset<mvcmovie.models.movie> movie { get; set; }
}
}

 

前面的代码为实体集创建 dbset<movie> 属性。 在实体框架术语中,实体集通常与数据表相对应。 实体对应表中的行。
通过调用 dbcontextoptions 对象中的一个方法将连接字符串名称传递到上下文。 进行本地开发时, asp.net core 配置系统
在 appsettings.json 文件中读取数据库连接字符串。

 

测试应用

运行应用并将 /movies 追加到浏览器中的 url (http://localhost:port/movies)。

如果收到如下所示数据库异常:缺少迁移步骤。
sqlexception: cannot open database “mvcmoviecontext-guid” requested by the login. the login failed.
login failed for user ‘user-name’.

检查 startup 类:configureservices方法
services.adddbcontext<mvcmoviecontext>(options =>
options.usesqlserver(configuration.getconnectionstring(“mvcmoviecontext”)));
上面突出显示的代码显示了要添加到依赖关系注入容器的电影数据库上下文:
services.adddbcontext<mvcmoviecontext>(options => 指定要使用的数据库和连接字符串。
=> 是 lambda 运算符

打开 controllers/moviescontroller.cs 文件并检查构造函数:

public class moviescontroller : controller
{
private readonly mvcmoviecontext _context;

public moviescontroller(mvcmoviecontext context)
{
_context = context;
}

构造函数使用依赖关系注入将数据库上下文 (mvcmoviecontext) 注入到控制器中。 数据库上下文将在控制器中的每个 crud 方法中使用。

强类型模型和 @model 关键词

mvc 还提供将强类型模型对象传递给视图的功能。 凭借此强类型方法可更好地对代码进行编译时检查。 基架机制在创建方法和视图时,
通过 moviescontroller 类和视图使用了此方法(即传递强类型模型)。
检查 controllers/moviescontroller.cs 文件中生成的 details 方法:

// get: movies/details/5
public async task<iactionresult> details(int? id)
{
if (id == null)
{
return notfound();
}

var movie = await _context.movie
.firstordefaultasync(m => m.id == id);
if (movie == null)
{
return notfound();
}

return view(movie);
}

id 参数通常作为路由数据传递。 例如 https://localhost:5001/movies/details/1 的设置如下:
控制器被设置为 movies 控制器(第一个 url 段)。
操作被设置为 details(第二个 url 段)。
id 被设置为 1(最后一个 url 段)。
还可以使用查询字符串传入 id,如下所示:

https://localhost:5001/movies/details?id=1

 

在未提供 id 值的情况下,id 参数可定义为可以为 null 的类型 (int?)。
lambda 表达式会被传入 firstordefaultasync 以选择与路由数据或查询字符串值相匹配的电影实体。

var movie = await _context.movie
.firstordefaultasync(m => m.id == id);

如果找到了电影,movie 模型的实例则会被传递到 details 视图:

return view(movie);
检查 views/movies/details.cshtml 文件的内容:
@model mvcmovie.models.movie

@{
viewdata[“title”] = “details”;
}

<h1>details</h1>

<div>
<h4>movie</h4>
<hr />
<dl class=”row”>
<dt class=”col-sm-2″>
@html.displaynamefor(model => model.title)
</dt>
<dd class=”col-sm-10″>
@html.displayfor(model => model.title)
</dd>
<dt class=”col-sm-2″>
@html.displaynamefor(model => model.releasedate)
</dt>
<dd class=”col-sm-10″>
@html.displayfor(model => model.releasedate)
</dd>
<dt class=”col-sm-2″>
@html.displaynamefor(model => model.genre)
</dt>
<dd class=”col-sm-10″>
@html.displayfor(model => model.genre)
</dd>
<dt class=”col-sm-2″>
@html.displaynamefor(model => model.price)
</dt>
<dd class=”col-sm-10″>
@html.displayfor(model => model.price)
</dd>
</dl>
</div>
<div>
<a asp-action=”edit” asp-route-id=”@model.id”>edit</a> |
<a asp-action=”index”>back to list</a>
</div>

 

 

通过将 @model 语句包括在视图文件的顶端,可以指定视图期望的对象类型。
创建电影控制器时,会自动在 details.cshtml 文件的顶端包括以下 @model 语句:
@model mvcmovie.models.movie
此 @model 指令使你能够使用强类型的 model 对象访问控制器传递给视图的电影。 例如,在 details.cshtml 视图中,
代码通过强类型的 model 对象将每个电影字段传递给 displaynamefor 和 displayforhtml 帮助程序。
create 和 edit 方法以及视图也传递一个 movie 模型对象。

检查电影控制器中的 index.cshtml 视图和 index 方法。 请注意代码在调用 view 方法时是如何创建 list 对象的。
代码将此 movies 列表从 index 操作方法传递给视图:

 

// get: movies
public async task<iactionresult> index()
{
return view(await _context.movie.tolistasync());
}

 

创建电影控制器时,基架会自动在 index.cshtml 文件的顶端包含以下 @model 语句:
@model ienumerable<mvcmovie.models.movie>

@model 指令使你能够使用强类型的 model 对象访问控制器传递给视图的电影列表。 例如,在 index.cshtml 视图中,
代码使用 foreach 语句通过强类型 model 对象对电影进行循环遍历:

 

@model ienumerable<mvcmovie.models.movie> \\

@{
viewdata[“title”] = “index”;
}

<h1>index</h1>

<p>
<a asp-action=”create”>create new</a>
</p>
<table class=”table”>
<thead>
<tr>
<th>
@html.displaynamefor(model => model.title)
</th>
<th>
@html.displaynamefor(model => model.releasedate)
</th>
<th>
@html.displaynamefor(model => model.genre)
</th>
<th>
@html.displaynamefor(model => model.price)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in model) {\\
<tr>
<td>
@html.displayfor(modelitem => item.title)\\
</td>
<td>
@html.displayfor(modelitem => item.releasedate)\\
</td>
<td>
@html.displayfor(modelitem => item.genre)\\
</td>
<td>
@html.displayfor(modelitem => item.price)\\
</td>
<td>
<a asp-action=”edit” asp-route-id=”@item.id”>edit</a> |\\
<a asp-action=”details” asp-route-id=”@item.id”>details</a> |\\
<a asp-action=”delete” asp-route-id=”@item.id”>delete</a>\\
</td>
</tr>
}
</tbody>
</table>

 

因为 model 对象为强类型(作为 ienumerable<movie> 对象),因此循环中的每个项都被类型化为 movie。
除其他优点之外,这意味着可对代码进行编译时检查: