在本教程之前,创建的所有asp.net mvc应用程序中,我们一直在将来自控制器的硬编码数据传递给视图模板。 但是,构建真正的web应用程序,可能需要使用真实的数据库。 在本章中,我们将学习如何使用数据库引擎来存储和检索应用程序所需的数据。
要存储和检索数据,我们将使用entity
框架的.net framework数据访问技术来定义和使用模型。
entity
框架(ef)支持code first技术,该技术允许通过编写简单的类来创建模型对象,然后从类中随时创建数据库,从而实现非常干净和快速的开发流程。
下面来看一个简单的例子,我们将在这个例子中添加entity
框架的支持来访问数据库。
第1步 - 创建一个项目:mvcdatabaseaccess,如下所示 -
要安装entity framework
框架,右键单击项目,然后选择管理nuget程序包 ,在弹出的界面中搜索:entity framework,如下图所示 -
选择entity framework,然后点击“install”按钮。它将打开预览对话框 -
接受安装协议,如下图所示 -
当entity framework框架安装成功,会看到下面的截图中所示的绿色“勾”的图标。如下图所示 -
我们需要向employee
模型添加另一个类,该类将与entity framework进行通信,以使用以下代码检索和保存数据。
using system;
using system.collections.generic;
using system.data.entity;
using system.linq;
using system.web;
namespace mvcdatabaseaccess.models
{
public class employee
{
public int id { get; set; }
public string name { get; set; }
public datetime joiningdate { get; set; }
public int age { get; set; }
}
public class empdbcontext : dbcontext
{
public empdbcontext()
{ }
public dbset<employee> employees { get; set; }
}
}
如上所示,empdbcontext
是从一个名为dbcontext的ef
类派生的。在这个类中有一个名为dbset
的属性它基本上表示想查询和保存的实体。
我们需要在web.config
文件中的<configuration>
标记下为数据库指定连接字符串。
<connectionstrings>
<add name = "empdbcontext" connectionstring = "data source=my-pc;initial catalog=testdb;integrated security=true" providername = "system.data.sqlclient"/>
</connectionstrings>
实际上不需要添加empdbcontext
连接字符串。如果不指定连接字符串,则entity framework将使用dbcontext
类的标准名称在用户目录中创建localdb
数据库。 对于这个示例,我们不会添加连接字符串来简化。
现在需要更新employeecontroller.cs
文件,以便可以从数据库中保存和检索数据,而不是使用硬编码的数据。
首先,添加创建一个私有的empdbcontext
类对象,然后更新index,create和edit动作方法,如下面的代码所示。参考以下代码 -
using mvcdatabaseaccess.models;
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.mvc;
namespace mvcdatabaseaccess.controllers
{
public class employeecontroller : controller
{
private empdbcontext db = new empdbcontext();
// get: employee
public actionresult index()
{
var employees = from e in db.employees
orderby e.id
select e;
return view(employees);
}
// get: employee/create
public actionresult create()
{
return view();
}
// post: employee/create
[httppost]
public actionresult create(employee emp)
{
try
{
db.employees.add(emp);
db.savechanges();
return redirecttoaction("index");
}
catch
{
return view();
}
}
// get: employee/edit/5
public actionresult edit(int id)
{
var employee = db.employees.single(m => m.id == id);
return view(employee);
}
// post: employee/edit/5
[httppost]
public actionresult edit(int id, formcollection collection)
{
try
{
var employee = db.employees.single(m => m.id == id);
if (tryupdatemodel(employee))
{
//to do:- database code
db.savechanges();
return redirecttoaction("index");
}
return view(employee);
}
catch
{
return view();
}
}
}
}
然后访问以下url:http://localhost:61868/employee
运行这个应用程序。将看到以下输出。
正如所看到的,这个视图上没有数据,但是程序已经自动创建了一个表:employee,这是因为我们还没有在数据库表中添加任何记录。
进入sql server对象资源管理器,会看到数据库使用与我们在dbcontext
类中创建的相同的名称 - employee
展开这个数据库,会看到它有一个包含employee
模型类中的所有字段的表。
要查看此表中的数据,请右键单击employees
表并选择查看数据。应该会看到目前没有数据记录。我们直接在数据库的employee
表中添加一些记录,如下图所示 -
刷新浏览器,应该会看到数据现在已经更新到了视图中了。如下图所示 -
点击“create new” 链接,从浏览器中添加一条记录,它将显示创建视图。在下面的字段中添加一些数据。
点击create按钮,它会更新索引视图以及添加这个新的记录到数据库。
现在打开sql server对象资源管理器并刷新数据库。右键单击employees
表并选择查看数据菜单选项。应该就会看到该记录被添加到数据库中了。