学习ASP .NET MVC5官方教程总结(九)添加新字段

在本章中,,我们将使用EntityFrameworkCodeFirst数据迁移功能将模型类的改变应用到数据库中。

在解决方案资源管理器中,删除自动创建的Movies.mdf文件。

在Enable-Migrations-ContextTypeNameMvcMovie.Models.MovieDBContext

Enable-Migrations命令创建了一个Migrations文件夹和Configuration.cs文件。

打开

protected override void Seed(MvcMovie.Models.MovieDBContext context){context.Movies.AddOrUpdate(i => i.Title,new Movie{Title = "When Harry Met Sally",ReleaseDate = DateTime.Parse("1989-1-11"),Genre = "Romantic Comedy",Price = 7.99M},new Movie{Title = "Ghostbusters ",ReleaseDate = DateTime.Parse("1984-3-13"),Genre = "Comedy",Price = 8.99M},new Movie{Title = "Ghostbusters 2",ReleaseDate = DateTime.Parse("1986-2-23"),Genre = "Comedy",Price = 9.99M},new Movie{Title = "Rio Bravo",ReleaseDate = DateTime.Parse("1959-4-15"),Genre = "Western",Price = 3.99M} );}

使用这段代码的时候,需要添加usingMvcMovie.Models的引用。

CodeFirst

在进行下一步之前,先编译解决方案,否则下一步会出错误。

下一步,为初始化迁移创建一个DbMigration类。这次迁移创建一个新数据库,这也是我们为什么要删除之前的数据库的原因。

CodeFirstMigrations{DateStamp}_Initial.cs),这个类包含了创建数据库结构的代码。迁移文件的文件名以{DateStamp}_Initial.cs文件将会运行并创建数据库结构,然后将执行Seed方法将测试数据插入数据库中。

运行应用程序,浏览

字段,添加后的代码如下:

public class Movie{public int ID { get; set; }public string Title { get; set; }[Display(Name = "Release Date")][DataType(DataType.Date)]public DateTime ReleaseDate { get; set; }public string Genre { get; set; }public decimal Price { get; set; }public string Rating { get; set; }}

编译解决方案。

现在我们已经更新了Movie类,你还需要修改\Views\Movies\Index.cshtml和\Views\Movies\Create.cshtml视图。修改后的代码如下:

@model IEnumerable<MvcMovie.Models.Movie>@{ViewBag.Title = "Index";}<h2>Index</h2><p>@Html.ActionLink("Create New", "Create")@using (Html.BeginForm("Index", "Movies", FormMethod.Get)){<p>Genre: @Html.DropDownList("movieGenre", "All")Title: @Html.TextBox("SearchString") <br /><input type="submit" value="Filter" /></p>}</p><table class="table"><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>@Html.DisplayNameFor(model => model.Rating)</th></tr>@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>@Html.DisplayFor(modelItem => item.Rating)</td><td>@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |@Html.ActionLink("Details", "Details", new { id=item.ID }) |@Html.ActionLink("Delete", "Delete", new { id=item.ID })</td></tr>}</table>

修改后的Create.cshtml:

@model MvcMovie.Models.Movie@{ViewBag.Title = "Create";}<h2>Create</h2>@using (Html.BeginForm()){@Html.AntiForgeryToken()@Html.ValidationSummary(true)<fieldset class="form-horizontal"><legend>Movie</legend><div class="control-group">@Html.LabelFor(model => model.Title, new { @class = "control-label" })<div class="controls">@Html.EditorFor(model => model.Title) @Html.ValidationMessageFor(model => model.Title, null, new { @class = "help-inline" })</div></div><div class="control-group">@Html.LabelFor(model => model.ReleaseDate, new { @class = "control-label" })<div class="controls">@Html.EditorFor(model => model.ReleaseDate)@Html.ValidationMessageFor(model => model.ReleaseDate, null, new { @class = "help-inline" })</div></div><div class="control-group">@Html.LabelFor(model => model.Genre, new { @class = "control-label" })<div class="controls">@Html.EditorFor(model => model.Genre) @Html.ValidationMessageFor(model => model.Genre, null, new { @class = "help-inline" })</div></div><div class="control-group">@Html.LabelFor(model => model.Price, new { @class = "control-label" })<div class="controls">@Html.EditorFor(model => model.Price)@Html.ValidationMessageFor(model => model.Price, null, new { @class = "help-inline" })</div></div><div class="control-group">@Html.LabelFor(model => model.Rating, new { @class = "control-label" })<div class="controls">@Html.EditorFor(model => model.Rating) @Html.ValidationMessageFor(model => model.Rating, null, new { @class = "help-inline" })</div></div><div class="form-actions no-color"><input type="submit" value="Create" class="btn" /></div></fieldset>}<div> @Html.ActionLink("Back to List", "Index")</div>@section Scripts {@Scripts.Render("~/bundles/jqueryval")}

现在我们已经在程序中为

出现这个错误的原因是

解决这个问题有以下几种途径:

懂得接受失败的人,就是懂得人生真谛的人,

学习ASP .NET MVC5官方教程总结(九)添加新字段

相关文章:

你感兴趣的文章:

标签云: