MVC现在版本已经是5了 EF现在最新的应该是6.0.2了
开发工具是 Visual Studio2013
数据库是 SQL Server 2012
这些需要.NET Framework4.5 的支持
如果没安装的同学可以先安装一下.NET Framework4.5
首先打开我们的开发环境,
把项目创建好了以后,
当然我这里是我之前写好的,我就不创建了
项目目录结构
现在在Models文件夹下边创建一个
创建一个数据库 MVCtest_DB
USE [MVCtest_DB]
GO /****** Object: Table [dbo].[UserTab] Script Date: 2014/3/26 9:23:26 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO SET ANSI_PADDING ON
GO CREATE TABLE [dbo].[UserTab](
[UID] [int] IDENTITY(1,1) NOT NULL,
[UserName] [varchar](50) NULL,
[UserPassword] [varchar](50) NULL,
[status] [int] NULL,
CONSTRAINT [PK_UserTab] PRIMARY KEY CLUSTERED
(
[UID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO SET ANSI_PADDING OFF
GO
USE [MVCtest_DB]
GO /****** Object: Table [dbo].[RoleTab] Script Date: 2014/3/26 9:24:38 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO SET ANSI_PADDING ON
GO CREATE TABLE [dbo].[RoleTab](
[RID] [int] IDENTITY(1,1) NOT NULL,
[Role] [varchar](500) NULL,
CONSTRAINT [PK_RoleTab] PRIMARY KEY CLUSTERED
(
[RID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO SET ANSI_PADDING OFF
GO
创建两张表
到这里准备工作基本完成。
连接数据库
点击下一步
连接好了之后
完成!
在Controllers里边添加一个控制器 UserController
选择控制器
好了等着 vs自己创建框架 视图等
public class UserController : Controller
{
private MVCtest_DBEntities db = new MVCtest_DBEntities(); // GET: /User/
public async Task<ActionResult> Index()
{
return View(await db.UserTabs.ToListAsync());
} // GET: /User/Details/5
public async Task<ActionResult> Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
UserTab usertab = await db.UserTabs.FindAsync(id);
if (usertab == null)
{
return HttpNotFound();
}
return View(usertab);
} // GET: /User/Create
public ActionResult Create()
{
return View();
} // POST: /User/Create
// 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关
// 详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include="UID,UserName,UserPassword,status")] UserTab usertab)
{
if (ModelState.IsValid)
{
db.UserTabs.Add(usertab);
await db.SaveChangesAsync();
return RedirectToAction("Index");
} return View(usertab);
} // GET: /User/Edit/5
public async Task<ActionResult> Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
UserTab usertab = await db.UserTabs.FindAsync(id);
if (usertab == null)
{
return HttpNotFound();
}
return View(usertab);
} // POST: /User/Edit/5
// 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关
// 详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include="UID,UserName,UserPassword,status")] UserTab usertab)
{
if (ModelState.IsValid)
{
db.Entry(usertab).State = EntityState.Modified;
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(usertab);
} // GET: /User/Delete/5
public async Task<ActionResult> Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
UserTab usertab = await db.UserTabs.FindAsync(id);
if (usertab == null)
{
return HttpNotFound();
}
return View(usertab);
} // POST: /User/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DeleteConfirmed(int id)
{
UserTab usertab = await db.UserTabs.FindAsync(id);
db.UserTabs.Remove(usertab);
await db.SaveChangesAsync();
return RedirectToAction("Index");
} protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
这是他自己创建的代码
但让这个时间视图也创建好了
浏览器打开一下
当然增删改都不用你再去动手 全都好了
是不是很快速啊!
如果觉得有帮助记得推荐一下 支持我一下