MVC5+EF6简单实例---以原有SQLServer数据库两表联合查询为例
Posted code、sky
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MVC5+EF6简单实例---以原有SQLServer数据库两表联合查询为例相关的知识,希望对你有一定的参考价值。
工具:VS.net2013、EF6、MVC5、SQLServer2008
参考出处:
http://www.cnblogs.com/slark/p/mvc-5-get-started-create-project.html
http://www.cnblogs.com/miro/p/4288184.html
http://www.cnblogs.com/dotnetmvc/p/3732029.html
一、准备工作
在SqlServer上创建数据库:Element
模拟两个表并插入数据:SysUser(用户表)、SysRole(角色表)
CREATE TABLE [dbo].[SysUser](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nchar](10) NOT NULL,
[RoleNum] [nchar](10) NOT NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[SysRole](
[ID] [int] IDENTITY(1,1) NOT NULL,
[RoleName] [nchar](10) NOT NULL,
[RoleNum] [nchar](10) NOT NULL
) ON [PRIMARY]
插入数据:
![](https://image.cha138.com/20210612/6396fcd9f0d6465496bd9d9639f7644e.jpg)
![](https://image.cha138.com/20210612/e2615de8e71b446a9789390808a9f57d.jpg)
![](https://image.cha138.com/20210612/01e09b47247845698a9f1704a272ffa3.jpg)
![](https://image.cha138.com/20210612/52a77a6dba9b473590138db3f651f42c.jpg)
![](https://image.cha138.com/20210612/6fafdc244c05414d95f9066e5c890b80.jpg)
二、使用EF的Code First从原有数据库中生成Models
![](https://image.cha138.com/20210612/88baf1b044c34510b8a9cebf81a1e339.jpg)
![](https://image.cha138.com/20210612/61df63e157fd4d28abdadfadcc52c31e.jpg)
![](https://image.cha138.com/20210612/9535ea0dcc6446e88a5ac0e097ca7780.jpg)
![](https://image.cha138.com/20210612/d1fa7d49e2d14094a946323d05e2bc40.jpg)
![](https://image.cha138.com/20210612/c43d5a195b4a403396572516f28e3a42.jpg)
![](https://image.cha138.com/20210612/c262d0c2d96b47208896decd878ef116.jpg)
![](https://image.cha138.com/20210612/517f083124404345ae0cb02be5862ca4.jpg)
![](https://image.cha138.com/20210612/5541be8650a742dd8e83305d76fb8de0.jpg)
![](https://image.cha138.com/20210612/8ebff0487e2048b69b976cfa66edd997.jpg)
![](https://image.cha138.com/20210612/2d33edf9f2ca4fcfbb3feedfb04f064f.jpg)
三、根据Model生成Controller及View
在Controllers文件夹上右键--添加--控制器
![](https://image.cha138.com/20210612/f0071661cafe4b63ab96497f6c30cd13.jpg)
![](https://image.cha138.com/20210612/c3d628c9fe244790898610aed96111d8.jpg)
![](https://image.cha138.com/20210612/8fbe658159344fa6b8543f9791628f71.jpg)
四、利用ViewModel显示多表联合查询
![](https://image.cha138.com/20210612/cbfa6dfa2de943bf8b277391c7b9845b.jpg)
namespace MVCDemo.ViewModels
{
public class UserRole
{
public string userName { get; set; }
public string userRole { get; set; }
}
}
右键Controllers文件夹添加控制类,此类继承于Controller类
using System;
using System.Collections.Generic;
using System.Linq; using System.Web;
using System.Web.Mvc; using System.Data.Entity;
using MVCDemo.ViewModels;
using MVCDemo.Models;
namespace MVCDemo.Controllers
{
public class UserRoleController : Controller
{
ElementModel db = new ElementModel();
public ActionResult Index()
{
var userRoleList = from uu in db.SysUsers
join ud in db.SysRoles on uu.RoleNum equals ud.RoleNum
where uu.ID == 1
select new UserRole {userName = uu.Name,userRole = ud.RoleName}
return View(userRoleList);
}
}
}
@model IEnumerable<MVCDemo.ViewModels.UserRole>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model=>model.userName)
</th>
<th>
@Html.DisplayNameFor(model => model.userRole)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.userName)
</td>
<td>
@Html.DisplayFor(modelItem => item.userRole)
</td>
</tr>
}
</table>
以上是关于MVC5+EF6简单实例---以原有SQLServer数据库两表联合查询为例的主要内容,如果未能解决你的问题,请参考以下文章
构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统(63)-Excel导入和导出