在Visual Studio 下的ASP.NET MVC5项目中对MySQL数据库进行EF建模的方法

在ASP.NET项目中操作数据库时,都会用到EF框架进行建模。下面介绍在Visual Studio工具中使用EF建模的具体步骤。笔者使用的是Visual Studio 2019工具,同样在2017版本中也适用。数据库使用的是MySQL,如果是SQL Server的话,步骤会更加简便。

在此之前,需要先安装

MySQL for Visual Studio 1.2.8

MySQL Connector Net 6.10.8

在项目建立后,还要使用NuGet工具,导入包

MySql.Data.Entity 6.10.8

MySql.Data 6.10.8

EntityFramework

注意:MySQL Connector Net,MySql.Data.Entity,MySql.Data 三者的版本号要一致,否则会出现错误。

首先,建立一个MVC的空项目。在Controlers目录中建立一个控制器HomeControler.cs文件。内容如图:

 

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
}

这些代码会自动生成。

然后,右击Index(),选择添加视图的一项,按照默认的选项生成视图,在Views目录下生成了Home目录,里面有一个Index.cshtml文件。内容如图:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
  
       
   
</head>
<body>
    <div><div>
</body>
</html>

至此,已经有了一个控制器和一个对应的视图了。继续添加模型,在Moudels目录上右击,选择添加->新建项,如图:

在Visual Studio 下的ASP.NET MVC5项目中对MySQL数据库进行EF建模的方法

选择数据->ADO.NET 实体数据类型。

在Visual Studio 下的ASP.NET MVC5项目中对MySQL数据库进行EF建模的方法

选择来自数据库的EF设计器,下一步

在Visual Studio 下的ASP.NET MVC5项目中对MySQL数据库进行EF建模的方法

如果没有连接先建立连接,选择"是"的选项,下一步

在Visual Studio 下的ASP.NET MVC5项目中对MySQL数据库进行EF建模的方法

选择数据库中的要建模的项,点击“完成”。稍后在Models目录中,可以看见生成的相关的文件。

在Visual Studio 下的ASP.NET MVC5项目中对MySQL数据库进行EF建模的方法

至此,模型已经建立完毕。也生成了操作数据库的相关实体类。下面会简单举例使用这些函数。

  1. 在网页上打印数据库中的表格

以名为student的表格为例,在HomeControler中添加方法Students()

public class HomeController : Controller
{
    private studentdbEntities studentdb = new studentdbEntities();
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
    //GET:Students
    public ActionResult Students()
    {
        return View(studentdb.student.ToList());
    }
}

studentdbEntities类是有EF生成的类,用来实例化一个连接对象。该对象可以访问到某一数据库中的表格,并对其进行操作。本示例中我们将数据库中的student表转化为List,并将此List传送给视图模型。转化为List后,List所有的方法均可以使用。对应的视图文件Students.cshtml中,内容如下:

@{
    Layout = null;
}
@using StudentMVC.Models
@model IEnumerable<student>

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>学生表</title>
</head>
<body>
    <div>
        <table border="1" cellpadding="3" cellspacing="0" style="width:60%;margin:auto">
            <tr>
                <td align="center" colspan="5">学生表</td>
            </tr>
            <tr>
                <td align="center">学号</td>
                <td align="center">姓名</td>
                <td align="center">性别</td>
                <td align="center">学龄</td>
                <td align="center">所在系</td>
            </tr>
            @foreach (student s in Model)
            {
                <tr>
                    <td align="center">@s.Sno</td>
                    <td align="center">@s.Sname</td>
                    <td align="center">@s.Ssex</td>
                    <td align="center">@s.Sage</td>
                    <td align="center">@s.Sdept</td>
                </tr>
            }
        </table>
    </div>
</body>
</html>

该视图使用的是Razor引擎解析,混杂着C#代码,通过foreach循环,在网页上动态生成了表格,表格中的内容对应这数据库的内容。启动项目后,访问域名~/Home/Students,可以看见效果:

在Visual Studio 下的ASP.NET MVC5项目中对MySQL数据库进行EF建模的方法