ASP.NET? MVC—EF实体模型
课程作业项目实践操作过程
操作步骤
1创建MVC项目并取名为“WEIphotoText”


2链接数据库,绑定Model数据
选择Model文件夹,点击鼠标右键添加新建项

?选择数据添加实体类,模型取名为Photo

?点击添加

下一步,新建连接

?选择个人计算机SQL server的数据库服务器名称

选择登录方式,要引用的数据库

测试连接,显示连接成功

?点击确定,下一步

选择实体框架,选择要引用的表,这里选择两个

点击完成
两个表就引用进来了,自动生成了实体模型
?3添加Photo控制器


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WEIphotoText.Models; //引用模型
namespace WEIphotoText.Controllers
{
public class PhotoController : Controller
{
WEIphotoDBEntities db = new WEIphotoDBEntities(); //创建数据库实体对象
// GET: Photo
public ActionResult Index()
{
List<Photograph> list = db.Photographs.ToList();
return View(list);
}
// GET: Photo/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: Photo/Create
public ActionResult Create()
{
return View();
}
// POST: Photo/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Photo/Edit/5
public ActionResult Edit(int id)
{
return View();
}
// POST: Photo/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Photo/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: Photo/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
?添加index视图,选择list模板,选择类
@model IEnumerable<WEIphotoText.Models.Photograph>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.photographLevelId)
</th>
<th>
@Html.DisplayNameFor(model => model.photographName)
</th>
<th>
@Html.DisplayNameFor(model => model.LengthPixel)
</th>
<th>
@Html.DisplayNameFor(model => model.widthPixel)
</th>
<th>
@Html.DisplayNameFor(model => model.photographIntroduction)
</th>
<th>
@Html.DisplayNameFor(model => model.ShootTime)
</th>
<th>
@Html.DisplayNameFor(model => model.userId)
</th>
<th>
@Html.DisplayNameFor(model => model.photographValue)
</th>
<th>
@Html.DisplayNameFor(model => model.downloadRight)
</th>
<th>
@Html.DisplayNameFor(model => model.useRight)
</th>
<th>
@Html.DisplayNameFor(model => model.photographlikeCount)
</th>
<th>
@Html.DisplayNameFor(model => model.downloadCount)
</th>
<th>
@Html.DisplayNameFor(model => model.phoCommentCount)
</th>
<th>
@Html.DisplayNameFor(model => model.phoUploadTime)
</th>
<th>
@Html.DisplayNameFor(model => model.photographIs_Del)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.photographLevelId)
</td>
<td>
@Html.DisplayFor(modelItem => item.photographName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LengthPixel)
</td>
<td>
@Html.DisplayFor(modelItem => item.widthPixel)
</td>
<td>
@Html.DisplayFor(modelItem => item.photographIntroduction)
</td>
<td>
@Html.DisplayFor(modelItem => item.ShootTime)
</td>
<td>
@Html.DisplayFor(modelItem => item.userId)
</td>
<td>
@Html.DisplayFor(modelItem => item.photographValue)
</td>
<td>
@Html.DisplayFor(modelItem => item.downloadRight)
</td>
<td>
@Html.DisplayFor(modelItem => item.useRight)
</td>
<td>
@Html.DisplayFor(modelItem => item.photographlikeCount)
</td>
<td>
@Html.DisplayFor(modelItem => item.downloadCount)
</td>
<td>
@Html.DisplayFor(modelItem => item.phoCommentCount)
</td>
<td>
@Html.DisplayFor(modelItem => item.phoUploadTime)
</td>
<td>
@Html.DisplayFor(modelItem => item.photographIs_Del)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.photographId }) |
@Html.ActionLink("Details", "Details", new { id=item.photographId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.photographId })
</td>
</tr>
}
</table>
浏览器中查看

?相关数据表

|