|
@@ -0,0 +1,161 @@
|
|
1
|
+> 添加数据模型类
|
|
2
|
+- 模型类,在Model中添加。例如Movie类
|
|
3
|
+```
|
|
4
|
+using System;
|
|
5
|
+using System.ComponentModel.DataAnnotations;
|
|
6
|
+
|
|
7
|
+namespace MvcMovie.Models
|
|
8
|
+{
|
|
9
|
+ public class Movie
|
|
10
|
+ {
|
|
11
|
+ public int Id { get; set; }
|
|
12
|
+ public string Title { get; set; }
|
|
13
|
+
|
|
14
|
+ [DataType(DataType.Date)]
|
|
15
|
+ public DateTime ReleaseDate { get; set; }
|
|
16
|
+ public string Genre { get; set; }
|
|
17
|
+ public decimal Price { get; set; }
|
|
18
|
+ }
|
|
19
|
+}
|
|
20
|
+```
|
|
21
|
+> 脚手架电影模型
|
|
22
|
+- 电影模型是搭建的。也就是说,脚手架工具为电影模型生成用于创建,读取,更新和删除(CRUD)操作的页面。
|
|
23
|
+- 在Solution Explorer中,右键单击Controllers文件夹> Add> New Scaffolded Item
|
|
24
|
+- 在Add Scaffold对话框中,使用Entity Framework> Add选择带视图的MVC Controller。
|
|
25
|
+- 完成添加控制器对话框:
|
|
26
|
+ - 型号类: 电影(MvcMovie.Models)
|
|
27
|
+ - 数据上下文类:选择+图标并添加默认的MvcMovie.Models.MvcMovieContext
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+> 初始迁移数据库
|
|
31
|
+
|
|
32
|
+- 添加初始迁移
|
|
33
|
+ - 从Tools菜单中,选择NuGet Package Manager > Package Manager Console(PMC)。
|
|
34
|
+ - 在PMC中,输入以下命令:Add-Migration Initial / Update-Database
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+> 在.net core 中使用数据库
|
|
38
|
+- 该MvcMovieContext对象处理连接到数据库和将Movie对象映射到数据库记录的任务。数据库上下文在Startup.cs文件的方法中使用Dependency Injection容器注册:ConfigureServices
|
|
39
|
+```
|
|
40
|
+public void ConfigureServices(IServiceCollection services)
|
|
41
|
+{
|
|
42
|
+ services.Configure<CookiePolicyOptions>(options =>
|
|
43
|
+ {
|
|
44
|
+ // This lambda determines whether user consent for non-essential cookies
|
|
45
|
+ // is needed for a given request.
|
|
46
|
+ options.CheckConsentNeeded = context => true;
|
|
47
|
+ options.MinimumSameSitePolicy = SameSiteMode.None;
|
|
48
|
+ });
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+ services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
|
|
52
|
+
|
|
53
|
+ services.AddDbContext<MvcMovieContext>(options =>
|
|
54
|
+ options.UseSqlServer(Configuration.GetConnectionString("MvcMovieContext")));
|
|
55
|
+}
|
|
56
|
+```
|
|
57
|
+
|
|
58
|
+- ASP.NET核心配置系统读取ConnectionString。对于本地开发,它从appsettings.json文件获取连接字符串:
|
|
59
|
+```
|
|
60
|
+"ConnectionStrings": {
|
|
61
|
+ "MvcMovieContext": "Server=(localdb)\\mssqllocaldb;Database=MvcMovieContext-2;Trusted_Connection=True;MultipleActiveResultSets=true"
|
|
62
|
+}
|
|
63
|
+```
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+> 种子数据库
|
|
68
|
+- 创建一个SeedData在Models文件夹中命名的新类
|
|
69
|
+```
|
|
70
|
+using Microsoft.EntityFrameworkCore;
|
|
71
|
+using Microsoft.Extensions.DependencyInjection;
|
|
72
|
+using System;
|
|
73
|
+using System.Linq;
|
|
74
|
+
|
|
75
|
+namespace MvcMovie.Models
|
|
76
|
+{
|
|
77
|
+ public static class SeedData
|
|
78
|
+ {
|
|
79
|
+ public static void Initialize(IServiceProvider serviceProvider)
|
|
80
|
+ {
|
|
81
|
+ using (var context = new MvcMovieContext(
|
|
82
|
+ serviceProvider.GetRequiredService<
|
|
83
|
+ DbContextOptions<MvcMovieContext>>()))
|
|
84
|
+ {
|
|
85
|
+ // Look for any movies.
|
|
86
|
+ if (context.Movie.Any())
|
|
87
|
+ {
|
|
88
|
+ return; // DB has been seeded
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ context.Movie.AddRange(
|
|
92
|
+ new Movie
|
|
93
|
+ {
|
|
94
|
+ Title = "When Harry Met Sally",
|
|
95
|
+ ReleaseDate = DateTime.Parse("1989-2-12"),
|
|
96
|
+ Genre = "Romantic Comedy",
|
|
97
|
+ Price = 7.99M
|
|
98
|
+ },
|
|
99
|
+
|
|
100
|
+ new Movie
|
|
101
|
+ {
|
|
102
|
+ Title = "Ghostbusters ",
|
|
103
|
+ ReleaseDate = DateTime.Parse("1984-3-13"),
|
|
104
|
+ Genre = "Comedy",
|
|
105
|
+ Price = 8.99M
|
|
106
|
+ },
|
|
107
|
+
|
|
108
|
+ new Movie
|
|
109
|
+ {
|
|
110
|
+ Title = "Ghostbusters 2",
|
|
111
|
+ ReleaseDate = DateTime.Parse("1986-2-23"),
|
|
112
|
+ Genre = "Comedy",
|
|
113
|
+ Price = 9.99M
|
|
114
|
+ },
|
|
115
|
+
|
|
116
|
+ new Movie
|
|
117
|
+ {
|
|
118
|
+ Title = "Rio Bravo",
|
|
119
|
+ ReleaseDate = DateTime.Parse("1959-4-15"),
|
|
120
|
+ Genre = "Western",
|
|
121
|
+ Price = 3.99M
|
|
122
|
+ }
|
|
123
|
+ );
|
|
124
|
+ context.SaveChanges();
|
|
125
|
+ }
|
|
126
|
+ }
|
|
127
|
+ }
|
|
128
|
+}
|
|
129
|
+```
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+> 向模型中添加一个新字段
|
|
133
|
+- 将Rating属性添加到Models / Movie.cs:
|
|
134
|
+```
|
|
135
|
+public class Movie
|
|
136
|
+{
|
|
137
|
+ public int Id { get; set; }
|
|
138
|
+ public string Title { get; set; }
|
|
139
|
+
|
|
140
|
+ [Display(Name = "Release Date")]
|
|
141
|
+ [DataType(DataType.Date)]
|
|
142
|
+ public DateTime ReleaseDate { get; set; }
|
|
143
|
+ public string Genre { get; set; }
|
|
144
|
+
|
|
145
|
+ [Column(TypeName = "decimal(18, 2)")]
|
|
146
|
+ public decimal Price { get; set; }
|
|
147
|
+ public string Rating { get; set; }
|
|
148
|
+}
|
|
149
|
+```
|
|
150
|
+- 因为您已向类添加了新字段,所以Movie需要更新绑定白名单,以便包含此新属性。在MoviesController.cs中,更新[Bind]for Create和Editaction方法的Rating属性以包含属性:
|
|
151
|
+```
|
|
152
|
+[Bind("Id,Title,ReleaseDate,Genre,Price,Rating")]
|
|
153
|
+```
|
|
154
|
+- 更新视图模板,以便Rating在浏览器视图中显示,创建和编辑新属性。
|
|
155
|
+
|
|
156
|
+编辑/Views/Movies/Index.cshtml文件并添加一个Rating字段:
|
|
157
|
+- 使用字段更新/Views/Movies/Create.cshtmlRating。
|
|
158
|
+- 更新SeedData类,以便为新列提供值。示例更改如下所示,但您需要对每个更改进行此更改new Movie。
|
|
159
|
+
|
|
160
|
+- 在PMC中,输入以下命令:Add-Migration Rating/Update-Database.
|
|
161
|
+
|