Adding a model to an ASP.NET Core MVC app
在 asp.net core mvc
中添加三个model (模型)
2017-3-30 8 分钟阅读时间长度
正文内容

  1. Add a data model class
    加上2个数据模型类
  2. Scaffolding a controller
    控制器基架
  3. Add EF tooling and perform initial migration
    添加EF工具并做为主迁移
  4. Test the app
    测试应用
  5. Dependency Injection
    依靠注入
  6. Strongly typed models and the @model keyword
    强类型模型与 @model
    关键字
  7. Additional resources
    外加能源
    By Rick Anderson and Tom Dykstra
    In this section you’ll add some classes for managing movies in a
    database.
    在本节,大家将足够一些类用于在db中管理
    movies。
    These classes will be the “Model” part of the MVC app.
    这几个类是 MVC 中的
    Model部分。
    You’ll use these classes with the Entity Framework Core (EF Core) to
    work with a database.
    咱俩将运用 EF core
    在DB上操作那么些类与数量。
    EF Core is an object-relational mapping (ORM) framework that simplifies
    the data-access code that you have to write.
    EF core
    是一个指标关联映射框架,可以简化操作数据库所急需写的代码及代码量。
    For this tutorial you’ll use SQLite, but EF Core supports many database
    engines.
    本学科中大家运用
    SQLite数据库,当然EF core 扶助很多样数据库。
    The model classes you’ll create are known as POCO classes (from
    “plain-old CLR objects”) because they don’t have any dependency on EF
    Core.
    大家将创制一些简约模型类,因为她们对EF
    core 没有别的借助。
    They just define the properties of the data that will be stored in the
    database.
    仅在类中定义DB中所要存款和储蓄的字段属性。
    In this tutorial you’ll write the model classes first, and EF Core will
    create the database.
    在本教程中,咱们选拔 Code First
    情势,先定义类,再用EF core 成立DB.
    An alternate approach not covered here is to generate model classes from
    an already-existing database.
    另一种做法是DB First
    情势,用1个已存在的DB自动生成多少模型类。
    For information about that approach, see ASP.NET Core – Existing
    Database.
    查看文章 ASP.NET Core –
    Existing Database. 以博取越多关于DB First 情势的消息。
    Add a data model class
    加上二个数量模型类
    In Solution Explorer, right click the MvcMovie project > Add > New
    Folder. Name the folder Models.
    在消除方案财富管理器中,右击
    MvcMovie 项目 > Add > New Folder 菜单。并取名文件夹为 Models

    Right click the Models folder > Add > Class. Name the class Movie
    and add the following properties:
    右击 Models 文件夹 > Add
    > Class 菜单。命名类的名字为Movie
    ,并在中间添加如下的品质代码:

 

jQuery 1jQuery 2

 1 using System;
 2 
 3 namespace MvcMovie.Models
 4 {
 5     public class Movie
 6     {
 7         public int ID { get; set; }
 8         public string Title { get; set; }
 9         public DateTime ReleaseDate { get; set; }
10         public string Genre { get; set; }
11         public decimal Price { get; set; }
12     }
13 }

C# code

 

The ID field is required by the database for the primary key.
ID
字段是数据库供给必须有的主键。
Build the project to verify you don’t have any errors. You now have a
Model in your MVC app.
编译项目检查确认保障没有不当。将来我们在先后中就有了MVC中的M。
Scaffolding a controller
控制器基架
In Solution Explorer, right-click the Controllers folder > Add >
Controller.
在缓解方案能源管理器中,右击
Controllers 文件夹,选择 > Add > Controller 菜单。

jQuery 3

In the Add MVC Dependencies dialog, select Minimal Dependencies, and
select Add.
在Add MVC Dependencies
对话框中,选用 Minimal Dependencies 并点击Add 按钮。

jQuery 4

Visual Studio adds the dependencies needed to scaffold a controller, but
the controller itself is not created.
VS会添加控制器基架所需的依赖项,但控制器未来还未曾被创制。
The next invoke of > Add > Controller creates the controller.
继承点击 > Add >
Controller 菜单,会添加3个控制器。
In Solution Explorer, right-click the Controllers folder > Add >
Controller.
在化解方案财富管理器中,右击
Controllers 文件夹并精选> Add > Controller 菜单。

jQuery 5

In the Add Scaffold dialog, tap MVC Controller with views, using Entity
Framework > Add.
在 Add Scaffold 对话框中,点击
MVC Controller with views, using Entity Framework 选项并点击Add
按钮。

jQuery 6

Complete the Add Controller dialog:
完成 Add Controller
对话框:
• Model class: Movie (MvcMovie.Models)
Model class :Movie
(MvcMovie.Models)(前面儿添加的模型类)
• Data context class: Select the + icon and add the default
MvcMovie.Models.MvcMovieContext
Data context class :点击 +
按钮并累加暗中认可的 MvcMovie.Models.MvcMovieContext 。

jQuery 7

• Views: Keep the default of each option checked
Views:保持暗许的选项接纳情形
• Controller name: Keep the default MoviesController
Controller name:保持私下认可的名字
MoviesController 。
• Tap Add
点击 Add 按钮。

jQuery 8

Visual Studio creates:
VS将会创设如下项:
• An Entity Framework Core database context class
(Data/MvcMovieContext.cs)
3个 EF Core
数据上下文类:Data/MvcMovieContext.cs
• A movies controller (Controllers/MoviesController.cs)
三个操纵器类:Controllers/MoviesController.cs
• Razor view files for Create, Delete, Details, Edit and Index pages
(Views/Movies/\.cshtml*)
CRUD的视图像和文字件:Views/Movies/\.cshtml*
The automatic creation of the database context and CRUD (create, read,
update, and delete) action methods and views is known as scaffolding.
自动创制 DB上下文类,CRUD
控制器 方法及相应视图的工具是贰个 被称之为基架。
You’ll soon have a fully functional web application that lets you manage
a movie database.
你及时就会有二个一体化意义的web应用,能够让你管理贰个movie DB。
If you run the app and click on the Mvc Movie link, you’ll get an error
similar to the following:
假若你运营应用并点击 Mvc Movie
链接,你会看出如下一个荒唐提醒:

jQuery 9jQuery 10

An unhandled exception occurred while processing the request.
SqlException: Cannot open database "MvcMovieContext-<GUID removed>" 
requested by the login. The login failed.
Login failed for user Rick

Error txt

You need to create the database, and you’ll use the EF Core Migrations
feature to do that.
你要求创设一个DB,我们将用EF
Core的Migrations 性子来达成。
Migrations lets you create a database that matches your data model and
update the database schema when your data model changes.
Migrations
性情可以让你由项目中的Models创设一个DB,并且当model变化时会将转移同步到DB结构中。
Add EF tooling and perform initial migration
添加EF工具并执行初阶化迁移
In this section you’ll use the Package Manager Console (PMC) to:
在本节,大家将动用包管理控制台:
• Add the Entity Framework Core Tools package. This package is required
to add migrations and update the database.
添加 EF core
工具包。这么些包需求有migrations 及更新 db。
• Add an initial migration.
累加1个初阶化的migration
• Update the database with the initial migration.
用开端化的migration
更新数据库。
From the Tools menu, select NuGet Package Manager > Package Manager
Console.
在Tools 菜单,选择 NuGet
Package Manager > Package Manager Console 菜单:

jQuery 11

In the PMC, enter the following commands:
在 PMC
命令板上,输入下边的指令:

jQuery 12jQuery 13

Install-Package Microsoft.EntityFrameworkCore.Tools
Add-Migration Initial
Update-Database

PMC bash

The Add-Migration command creates code to create the initial database
schema.
Add-Migration
命令生成了开首化db结构的代码。
The schema is based on the model specified in the DbContext(In the
*Data/MvcMovieContext.cs file).
Db结构 基于专门创造的 DbContext
模型类。
The Initialargument is used to name the migrations. You can use any
name, but by convention you choose a name that describes the
migration.
Initial 参数用于命名migrations
。你能够在此处运用别的名字,可是习惯上要采用一个得以描述 migration
的名字。
See Introduction to migrations for more information.
查看 Introduction to migrations
能够获得更加多音信。
The Update-Database command runs the Up method in the
Migrations/<time-stamp>_InitialCreate.csfile, which creates the
database.
Update-Database 命令执行了
Migrations/<time-stamp>_InitialCreate.csfile 文件中的 Up
方法制造DB。
Test the app
测试应用
• Run the app and tap the Mvc Movie link.
运转应用并点击 Mvc Movie
链接。
• Tap the Create New link and create a movie.
点击 Create New
链接并创办一条电影。

jQuery 14

• You may not be able to enter decimal points or commas in the Price
field.
您可能无法在 Price字段里输入小数点或逗号。
To support jQuery validation for non-English locales that use a comma
(“,”) for a decimal point,
选拔 jQuery validation
辅助在非英文场景使用逗号与小数点,
and non US-English date formats, you must take steps to globalize your
app.
以及非美式日期格式,你需求做一些操作来全球化你的应用。
See Additional resourcesfor more information.
查阅 Additional resources
能够获取愈来愈多音讯。
For now, just enter whole numbers like 10.
近年来,笔者先只输入像10如此的平头。
• In some locales you need to specify the date format. See the
highlighted code below.
在某个场景,你要求特地钦定尤其的日子格式。看上边高亮的代码部分:

jQuery 15jQuery 16

 1 using System;
 2 using System.ComponentModel.DataAnnotations;
 3 
 4 namespace MvcMovie.Models
 5 {
 6     public class Movie
 7     {
 8         public int ID { get; set; }
 9         public string Title { get; set; }
10         [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
11         public DateTime ReleaseDate { get; set; }
12         public string Genre { get; set; }
13         public decimal Price { get; set; }
14     }
15 }

C# code

We’ll talk about DataAnnotations later in the tutorial.
我们将在本教程的前边讲解数据注脚。
Tapping Create causes the form to be posted to the server, where the
movie information is saved in a database.
点击 Create
将会向服务器发送表单,然后电影多少的音讯就会保留到数据库。
The app redirects to the /Movies URL, where the newly created movie
information is displayed.
利用重定向到 /Movies
地址,在那些页面,新创立的录制新闻就会展现出来。

jQuery 17

jQuery,Create a couple more movie entries. Try the Edit, Details, and Delete
links, which are all functional.
创办越来越多的录制条目,尝试 Edit,
Details, and Delete 链接操作,他们都是成效一体化的。
Dependency Injection
依靠注入
Open the Startup.cs file and examine ConfigureServices:
打开 Startup.cs 文件,并查看
ConfigureServices 方法:

 

jQuery 18jQuery 19

1 public void ConfigureServices(IServiceCollection services)
2 {
3     // Add framework services.
4     services.AddMvc();
5 
6     services.AddDbContext<MvcMovieContext>(options =>
7             options.UseSqlServer(Configuration.GetConnectionString("MvcMovieContext")));
8 }

C# code

 

The highlighted code above shows the movie database context being added
to the Dependency Injection container.
上边高亮的代码展现 movie
DB上下文已经添加到了DI容器中。
The line following services.AddDbContext<MvcMovieContext>(options
=> is not shown (see your code).
services.AddDbContext<MvcMovieContext>(options
=> 那行代码下的代码没有彰显(查看代码文件就能来看)。
It specifies the database to use and the connection string. => is a
lambda operator.
它内定了数据库使用的连接字符串。=>
符号是 lambda 操作符。
Open the Controllers/MoviesController.cs file and examine the
constructor:
开辟
Controllers/MoviesController.cs 文件并查阅构造函数:

 

jQuery 20jQuery 21

1 public class MoviesController : Controller
2 {
3     private readonly MvcMovieContext _context;
4 
5     public MoviesController(MvcMovieContext context)
6     {
7         _context = context;
8     }

C# code

 

The constructor uses Dependency Injection to inject the database context
(MvcMovieContext) into the controller.
构造函数使用 DI
注入DB上下文到控制器中。
The database context is used in each of the CRUD methods in the
controller.
DB上下文将会在控制器中的各类CRUD方法中动用。
Strongly typed models and the @model keyword
强类型Model与@model
关键字
Earlier in this tutorial, you saw how a controller can pass data or
objects to a view using the ViewDatadictionary.
在课程的前面部分,你早已了然哪些用
ViewData 字典将数据从控制器传递给视图使用。
The ViewData dictionary is a dynamic object that provides a convenient
late-bound way to pass information to a view.
ViewData
字典是1个动态指标,它提供了1个后绑定方式以传递数据到视图中。
MVC also provides the ability to pass strongly typed model objects to a
view.
MVC同时也提供了向视图传递强类型视图的章程。
This strongly typed approach enables better compile-time checking of
your code.
强类型在编译代码时能更好的检讨你的代码。
The scaffolding mechanism used this approach (that is, passing a
strongly typed model) with the MoviesController class and views when it
created the methods and views.
基架的体制使用的接近那种,在成立控制器方法与视图的时候。
Examine the generated Details method in the
Controllers/MoviesController.cs file:

Controllers/MoviesController.cs 文件中查阅自动生成的 Details
方法:

 

jQuery 22jQuery 23

 1 // GET: Movies/Details/5
 2 public async Task<IActionResult> Details(int? id)
 3 {
 4     if (id == null)
 5     {
 6         return NotFound();
 7     }
 8 
 9     var movie = await _context.Movie
10         .SingleOrDefaultAsync(m => m.ID == id);
11     if (movie == null)
12     {
13         return NotFound();
14     }
15 
16     return View(movie);
17 }

C# code

 

The id parameter is generally passed as route data. For example
http://localhost:5000/movies/details/1 sets:
id
参数常常是路由参数字传送递过来的。例如
http://localhost:5000/movies/details/1 中有多个路由段:
• The controller to the movies controller (the first URL segment).
率先段是 movies
控制器。
• The action to details (the second URL segment).
其次段是 details
action方法。
• The id to 1 (the last URL segment).
其三段是 id=1 参数。
You can also pass in the id with a query string as follows:
当然你也能够吧 id
参数放到查询字符串里面,如下U大切诺基L:
http://localhost:1234/movies/details?id=1
The id parameter is defined as a nullable type (int?) in case an ID
value is not provided.
id
参数被定义为了可空类型,以适应ID未提供的景色。
A lambda expression is passed in to SingleOrDefaultAsync to select movie
entities that match the route data or query string value.
贰个lambda 表明式 被做为
SingleOrDefaultAsync
方法的参数以找出相应的数额实体(与路由参数或询问字符串参数一致的的)。

jQuery 24jQuery 25

1 var movie = await _context.Movie
2 .SingleOrDefaultAsync(m => m.ID == id);

C# code

If a movie is found, an instance of the Movie model is passed to the
Details view:
假诺数额实体被找到,则会如代码所示的措施传递给视图:

jQuery 26jQuery 27

1 return View(movie);

C# code

Examine the contents of the Views/Movies/Details.cshtml file:
查看
Views/Movies/Details.cshtml 文件的始末,如下:

 

jQuery 28jQuery 29

 1 @model MvcMovie.Models.Movie
 2 
 3 @{
 4     ViewData["Title"] = "Details";
 5 }
 6 
 7 <h2>Details</h2>
 8 
 9 <div>
10     <h4>Movie</h4>
11     <hr />
12     <dl class="dl-horizontal">
13         <dt>
14             @Html.DisplayNameFor(model => model.Title)
15         </dt>
16         <dd>
17             @Html.DisplayFor(model => model.Title)
18         </dd>
19         <dt>
20             @Html.DisplayNameFor(model => model.ReleaseDate)
21         </dt>
22         <dd>
23             @Html.DisplayFor(model => model.ReleaseDate)
24         </dd>
25         <dt>
26             @Html.DisplayNameFor(model => model.Genre)
27         </dt>
28         <dd>
29             @Html.DisplayFor(model => model.Genre)
30         </dd>
31         <dt>
32             @Html.DisplayNameFor(model => model.Price)
33         </dt>
34         <dd>
35             @Html.DisplayFor(model => model.Price)
36         </dd>
37     </dl>
38 </div>
39 <div>
40     <a asp-action="Edit" asp-route-id="@Model.ID">Edit</a> |
41     <a asp-action="Index">Back to List</a>
42 </div>

HTML code

 

By including a @model statement at the top of the view file, you can
specify the type of object that the view expects.
通过在顶部涵盖一个@model
语句,你能够钦命视图所要使用的模子类型。
When you created the movie controller, Visual Studio automatically
included the following @model statement at the top of the Details.cshtml
file:
在你添加movie控制器时,vs
自动在Details.cshtml 文件的顶部包罗了@model 语句,如下所示:

jQuery 30jQuery 31

1 @model MvcMovie.Models.Movie

C# code

This @model directive allows you to access the movie that the controller
passed to the view by using a Model object that’s strongly typed.
@model
指令允许你在视图上以强类型的形式访问控制器传递过来的Model
对象。
For example, in the Details.cshtml view, the code passes each movie
field to the DisplayNameFor and DisplayFor HTML Helpers with the
strongly typed Modelobject.
譬如在Details.cshtml
视图中,代码用 DisplayNameFor 和DisplayFor 突显强类型Model
对象的每一个字段。
The Create and Edit methods and views also pass a Movie model object.
Create 和艾德it
同样在视图中分析Movie 模型对象。
Examine the Index.cshtml view and the Index method in the Movies
controller.
翻看Index.cshtml
视图及控制器中的 Index 方法。
Notice how the code creates a List object when it calls the View
method.
在意代码中在调用View
方法时是如何创立贰个List 集合对象的。
The code passes this Movies list from the Index action method to the
view:
代码中从Index 方法传递Movies
列表到视图中,如下:

jQuery 32jQuery 33

1 // GET: Movies
2 public async Task<IActionResult> Index()
3 {
4 return View(await _context.Movie.ToListAsync());
5 }

C# code

When you created the movies controller, scaffolding automatically
included the following @modelstatement at the top of the Index.cshtml
file:
当您创制movie控制器,基价自动会将如下代码放到Index.cshtml
文件的顶部:

jQuery 34jQuery 35

1 @model IEnumerable<MvcMovie.Models.Movie>

C# code

The @model directive allows you to access the list of movies that the
controller passed to the view by using a Model object that’s strongly
typed.
@model
指令允许你以强类型的艺术在视图上访问控制器传递过来的movies
列表。
For example, in the Index.cshtml view, the code loops through the movies
with a foreach statement over the strongly typed Model object:
诸如,在Index.cshtml
视图中,通过foreach 语句循环movie列表中的每一种强类型Model 对象:

 

jQuery 36jQuery 37

 1 @model IEnumerable<MvcMovie.Models.Movie>
 2 
 3 @{
 4     ViewData["Title"] = "Index";
 5 }
 6 
 7 <h2>Index</h2>
 8 
 9 <p>
10     <a asp-action="Create">Create New</a>
11 </p>
12 <table class="table">
13     <thead>
14         <tr>
15                 <th>
16                     @Html.DisplayNameFor(model => model.Title)
17                 </th>
18                 <th>
19                     @Html.DisplayNameFor(model => model.ReleaseDate)
20                 </th>
21                 <th>
22                     @Html.DisplayNameFor(model => model.Genre)
23                 </th>
24                 <th>
25                     @Html.DisplayNameFor(model => model.Price)
26                 </th>
27             <th></th>
28         </tr>
29     </thead>
30     <tbody>
31 @foreach (var item in Model) {
32         <tr>
33             <td>
34                 @Html.DisplayFor(modelItem => item.Title)
35             </td>
36             <td>
37                 @Html.DisplayFor(modelItem => item.ReleaseDate)
38             </td>
39             <td>
40                 @Html.DisplayFor(modelItem => item.Genre)
41             </td>
42             <td>
43                 @Html.DisplayFor(modelItem => item.Price)
44             </td>
45             <td>
46                 <a asp-action="Edit" asp-route-id="@item.ID">Edit</a> |
47                 <a asp-action="Details" asp-route-id="@item.ID">Details</a> |
48                 <a asp-action="Delete" asp-route-id="@item.ID">Delete</a>
49             </td>
50         </tr>
51 }
52     </tbody>
53 </table>

HTML code

Because the Model object is strongly typed (as an
IEnumerable<Movie> object), each item in the loop is typed as
Movie.
因为Model
对象是强类型的,循环中的每一个对象都以Movie 类型。
Among other benefits, this means that you get compile-time checking of
the code:
还有任何有点,比如您在编写翻译的同时也由编写翻译器自动物检疫查了代码的科学:

jQuery 38

Additional resources
其剩余资金源
• Tag Helpers
• Globalization and localization

 

 

                                         蒙

                                    2017-07-20
16:31 周四

                                    2017-07-21
 15:10  修正

 

相关文章

网站地图xml地图