当前位置:编程学习 > asp >>

微软ASP.NET站点部署指南(9):部署数据库更新

 

1.  综述

不管什么时候,程序都有可能像代码更新一样更新数据库。本章节你将进行数据库修改,测试,然后部署到测试环境和生产环境。

 

提醒:如果根据本章节所做的操作出现错误信息或一些功能不正常的话,请务必check Troubleshooting页面。

 

2.  给表添加新列

本小节,将修改Student和Instructor实体共同的基类Person类,添加一个birth date字段,也要在展示页面的表格上添加一个新列。

 

在ContosoUniversity.DAL项目,打开Person.cs在类的末尾添加如下的代码属性(记得,大括号里面哦)。

[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)] 

[Required(ErrorMessage = "Birth date is required.")] 

[Display(Name = "Birth Date")] 

public DateTime? BirthDate { get; set; } 

 

 

 

需要更新初始化类来支持新列,打开SchoolInitializer.cs文件找到var students = new List<Student>开头的代码,替换成包括birth date信息的如下的爱吗:

var students = new List<Student> 

    new Student { FirstMidName = "Carson",   LastName = "Alexander", EnrollmentDate = DateTime.Parse("2005-09-01"), BirthDate = DateTime.Parse("1990-01-01") }, 

    new Student { FirstMidName = "Meredith", LastName = "Alonso",    EnrollmentDate = DateTime.Parse("2002-09-01"), BirthDate = DateTime.Parse("1989-01-15") }, 

    new Student { FirstMidName = "Arturo",   LastName = "Anand",     EnrollmentDate = DateTime.Parse("2003-09-01"), BirthDate = DateTime.Parse("1988-02-01") }, 

    new Student { FirstMidName = "Gytis",    LastName = "Barzdukas", EnrollmentDate = DateTime.Parse("2002-09-01"), BirthDate = DateTime.Parse("1987-03-15") }, 

    new Student { FirstMidName = "Yan",      LastName = "Li",        EnrollmentDate = DateTime.Parse("2002-09-01"), BirthDate = DateTime.Parse("1985-11-11") }, 

    new Student { FirstMidName = "Peggy",    LastName = "Justice",   EnrollmentDate = DateTime.Parse("2001-09-01"), BirthDate = DateTime.Parse("1970-11-21") }, 

    new Student { FirstMidName = "Laura",    LastName = "Norman",    EnrollmentDate = DateTime.Parse("2003-09-01"), BirthDate = DateTime.Parse("1992-10-11") }, 

    new Student { FirstMidName = "Nino",     LastName = "Olivetto",  EnrollmentDate = DateTime.Parse("2005-09-01"), BirthDate = DateTime.Parse("1986-06-06") } 

 

}; 

 

 

 

将var instructors = new List<Instructor>开头的代码块替换成如下代码:

var instructors = new List<Instructor> 

    new Instructor { FirstMidName = "Kim",     LastName = "Abercrombie", HireDate = DateTime.Parse("1995-03-11"), BirthDate = DateTime.Parse("1918-08-12") }, 

    new Instructor { FirstMidName = "Fadi",    LastName = "Fakhouri",    HireDate = DateTime.Parse("2002-07-06"), BirthDate = DateTime.Parse("1960-03-15") }, 

    new Instructor { FirstMidName = "Roger",   LastName = "Harui",       HireDate = DateTime.Parse("1998-07-01"), BirthDate = DateTime.Parse("1970-01-11") }, 

    new Instructor { FirstMidName = "Candace", LastName = "Kapoor",      HireDate = DateTime.Parse("2001-01-15"), BirthDate = DateTime.Parse("1975-04-11") }, 

    new Instructor { FirstMidName = "Roger",   LastName = "Zheng",       HireDate = DateTime.Parse("2004-02-12"), BirthDate = DateTime.Parse("1957-10-12") } 

 

}; 

 

 

 

在ContosoUniversity项目打开Instructors.aspx文件,添加一个模板字段来显示birth date,在hire date和office assignment字段之间:

<asp:TemplateField HeaderText="Birth Date" SortExpression="BirthDate"> 

    <ItemTemplate> 

        <asp:Label ID="InstructorBirthDateLabel" runat="server" Text='<%# Eval("BirthDate", "{0:d}") %>'></asp:Label> 

    </ItemTemplate> 

    <EditItemTemplate> 

        <asp:TextBox ID="InstructorBirthDateTextBox" runat="server" Text='<%# Bind("BirthDate", "{0:d}") %>' 

            Width="7em"></asp:TextBox> 

    </EditItemTemplate> 

</asp:TemplateField> 

 

(注:如果代码变形了,可以通过CTRL+K+D来格式化代码)

 

在Students.aspx文件,在enrollment date 之前添加一个dynamic字段显示birth date:

<asp:DynamicField DataField="BirthDate" HeaderText="Birth Date" SortExpression="BirthDate"/> 

 

 

 

在StudentsAdd.aspx页面, enrollment date之前添加一个bound字段显示birth date:

<asp:BoundField DataField="BirthDate" HeaderText="Birth Date" 

    SortExpression="BirthDate"/> 

 

 

 

在BLL\SchoolBL.cs文件, 支持按列排序的GetStudents 方法力的switch语句里的default语句之前,添加一个case分支以支持使用birth date排序:

case "BirthDate": 

    students = students.OrderBy(s => s.BirthDate); 

    break; 

case "BirthDate DESC": 

    students = students.OrderByDescending(s => s.BirthDate); 

    break; 

 

 

 

运行程序访问Students页面,你可能感觉有一点慢,那是因为Entity Framework探测到了model变了,需要删除并重建数据库,然后将初始化里的数据插入到数据库。页面加载以后,可以看到有了一个birth date新列。

 

\

访问Add Students和Instructors页面验证是否有这个新列。

 

\

 

\

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,