04节-使用EF CodeFirst来插入数据表

04节-使用EF CodeFirst来插入数据表

新增一个account控制器,添加一个regist的action,同时创建regist的视图。

访问 http://localhost:55751/Account/Regist 就会跳到我们刚才的页面。

视图中添加下面的表单:

@model WebApplicationCF01.Models.Account

@{
ViewBag.Title = "Regist";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>博客注册</h2>

<form action="Regist" method="post">
@Html.EditorForModel()
<input type="submit" value="提交" />
</form>

然后在控制器里面添加一个接收的action:

public ActionResult Regist()
{
return View();
}

[HttpPost]
public ActionResult Regist(Account model)
{
return View();
}

使用CodeFirst实现插入操作:

//添加到数据表里面
context.account.Add(model);
context.SaveChanges();

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注