[C#/ASP.NET MVC] 엔티티 프레임워크(Entity Framework)를 사용해 ASP.NET MVC 애플리케이션 만들기
C#/ASP.NET MVC 2020. 10. 2. 13:20728x90
728x170
▶ Room.cs
using System.Collections.Generic;
namespace TestProject.Models
{
/// <summary>
/// 회의실
/// </summary>
public class Room
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 회의실 ID - RoomID
/// <summary>
/// 회의실 ID
/// </summary>
public int RoomID { get; set; }
#endregion
#region 명칭 - Name
/// <summary>
/// 명칭
/// </summary>
public string Name { get; set; }
#endregion
#region 스피커 컬렉션 - SpeakerCollection
/// <summary>
/// 스피커 컬렉션
/// </summary>
public virtual ICollection<Speaker> SpeakerCollection { get; set; }
#endregion
}
}
728x90
▶ Speaker.cs
namespace TestProject.Models
{
/// <summary>
/// 스피커
/// </summary>
public class Speaker
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 스피커 ID - SpeakerID
/// <summary>
/// 스피커 ID
/// </summary>
public int SpeakerID { get; set; }
#endregion
#region 명칭 - Name
/// <summary>
/// 명칭
/// </summary>
public string Name { get; set; }
#endregion
#region 회의실 ID - RoomID
/// <summary>
/// 회의실 ID
/// </summary>
public int RoomID { get; set; }
#endregion
#region 회의실 - Room
/// <summary>
/// 회의실
/// </summary>
public virtual Room Room { get; set; }
#endregion
}
}
300x250
▶ TestProjectContext.cs
using System.Data.Entity;
using TestProject.Models;
namespace TestProject.Data
{
/// <summary>
/// 테스트 프로젝트 컨텍스트
/// </summary>
public class TestProjectContext : DbContext
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 방 DB 집합 - RoomDBSet
/// <summary>
/// 방 DB 집합
/// </summary>
public DbSet<Room> RoomDBSet { get; set; }
#endregion
#region 스피커 DB 집합 - SpeakerDBSet
/// <summary>
/// 스피커 DB 집합
/// </summary>
public DbSet<Speaker> SpeakerDBSet { get; set; }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - TestProjectContext()
/// <summary>
/// 생성자
/// </summary>
public TestProjectContext() : base("name=TestProjectContext")
{
}
#endregion
}
}
▶ HomeController.cs
using System.Web.Mvc;
namespace TestProject.Controllers
{
/// <summary>
/// HOME 컨트롤러
/// </summary>
public class HomeController : Controller
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 인덱스 페이지 처리하기 - Index()
/// <summary>
/// 인덱스 페이지 처리하기
/// </summary>
/// <returns>액션 결과</returns>
public ActionResult Index()
{
return View();
}
#endregion
#region 소개 페이지 처리하기 - About()
/// <summary>
/// 소개 페이지 처리하기
/// </summary>
/// <returns>액션 결과</returns>
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
#endregion
#region 연락처 페이지 처리하기 - Contact()
/// <summary>
/// 연락처 페이지 처리하기
/// </summary>
/// <returns>액션 결과</returns>
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
#endregion
}
}
▶ RoomController.cs
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using TestProject.Data;
using TestProject.Models;
namespace TestProject.Controllers
{
/// <summary>
/// 회의실 컨트롤러
/// </summary>
public class RoomController : Controller
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 컨텍스트
/// </summary>
private TestProjectContext context = new TestProjectContext();
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 인덱스 페이지 처리하기 - Index() [GET : Room]
/// <summary>
/// 인덱스 페이지 처리하기
/// </summary>
/// <returns>액션 결과</returns>
public ActionResult Index()
{
return View(this.context.RoomDBSet.ToList());
}
#endregion
#region 상세 페이지 처리하기 - Details(id) [GET : Room/Details/5]
/// <summary>
/// 상세 페이지 처리하기
/// </summary>
/// <param name="id">ID</param>
/// <returns>액션 결과</returns>
public ActionResult Details(int? id)
{
if(id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Room room = this.context.RoomDBSet.Find(id);
if(room == null)
{
return HttpNotFound();
}
return View(room);
}
#endregion
#region 생성 페이지 처리하기 - Create() [GET : Room/Create]
/// <summary>
/// 생성 페이지 처리하기
/// </summary>
/// <returns>액션 결과</returns>
public ActionResult Create()
{
return View();
}
#endregion
#region 생성 페이지 처리하기 - Create(room) [POST : Room/Create]
/// <summary>
/// 생성 페이지 처리하기
/// </summary>
/// <param name="room">회의실</param>
/// <returns>액션 결과</returns>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "RoomID,Name")] Room room)
{
if(ModelState.IsValid)
{
this.context.RoomDBSet.Add(room);
this.context.SaveChanges();
return RedirectToAction("Index");
}
return View(room);
}
#endregion
#region 편집 페이지 처리하기 - Edit(id) [GET : Room/Edit/5]
/// <summary>
/// 편집 페이지 처리하기
/// </summary>
/// <param name="id">ID</param>
/// <returns>액션 결과</returns>
public ActionResult Edit(int? id)
{
if(id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Room room = this.context.RoomDBSet.Find(id);
if(room == null)
{
return HttpNotFound();
}
return View(room);
}
#endregion
#region 편집 페이지 처리하기 - Edit(room) [POST : Room/Edit/5]
/// <summary>
/// 편집 페이지 처리하기
/// </summary>
/// <param name="room">회의실</param>
/// <returns>액션 결과</returns>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "RoomID,Name")] Room room)
{
if(ModelState.IsValid)
{
this.context.Entry(room).State = EntityState.Modified;
this.context.SaveChanges();
return RedirectToAction("Index");
}
return View(room);
}
#endregion
#region 삭제 페이지 처리하기 - Delete(id) [GET : Room/Delete/5]
/// <summary>
/// 삭제 페이지 처리하기
/// </summary>
/// <param name="id">ID</param>
/// <returns>액션 결과</returns>
public ActionResult Delete(int? id)
{
if(id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Room room = this.context.RoomDBSet.Find(id);
if(room == null)
{
return HttpNotFound();
}
return View(room);
}
#endregion
#region 확인시 삭제 페이지 처리하기 - DeleteConfirmed(id) [POST : Room/Delete/5]
/// <summary>
/// 확인시 삭제 페이지 처리하기
/// </summary>
/// <param name="id">ID</param>
/// <returns>액션 결과</returns>
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Room room = this.context.RoomDBSet.Find(id);
this.context.RoomDBSet.Remove(room);
this.context.SaveChanges();
return RedirectToAction("Index");
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Protected
#region 리소스 해제하기 - Dispose(disposing)
/// <summary>
/// 리소스 해제하기
/// </summary>
/// <param name="disposing">리소스 해제 여부</param>
protected override void Dispose(bool disposing)
{
if(disposing)
{
this.context.Dispose();
}
base.Dispose(disposing);
}
#endregion
}
}
▶ SpeakerController.cs
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using TestProject.Data;
using TestProject.Models;
namespace TestProject.Controllers
{
/// <summary>
/// 스피커 컨트롤러
/// </summary>
public class SpeakerController : Controller
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 컨텍스트
/// </summary>
private TestProjectContext context = new TestProjectContext();
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 인덱스 페이지 처리하기 - Index() [GET : Speaker]
/// <summary>
/// 인덱스 페이지 처리하기
/// </summary>
/// <returns>액션 결과</returns>
public ActionResult Index()
{
IQueryable<Speaker> speakerQueryable = this.context.SpeakerDBSet.Include(s => s.Room);
return View(speakerQueryable.ToList());
}
#endregion
#region 상세 페이지 처리하기 - Details(id) [GET : Speaker/Details/5]
/// <summary>
/// 상세 페이지 처리하기
/// </summary>
/// <param name="id">ID</param>
/// <returns>액션 결과</returns>
public ActionResult Details(int? id)
{
if(id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Speaker speaker = this.context.SpeakerDBSet.Find(id);
if(speaker == null)
{
return HttpNotFound();
}
return View(speaker);
}
#endregion
#region 생성 페이지 처리하기 - Create() [GET : Speaker/Create]
/// <summary>
/// 생성 페이지 처리하기
/// </summary>
/// <returns>액션 결과</returns>
public ActionResult Create()
{
ViewBag.RoomID = new SelectList(this.context.RoomDBSet, "RoomID", "Name");
return View();
}
#endregion
#region 생성 페이지 처리하기 - Create(speaker) [POST : Speaker/Create]
/// <summary>
/// 생성 페이지 처리하기
/// </summary>
/// <param name="speaker">스피커</param>
/// <returns>액션 결과</returns>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "SpeakerID,Name,RoomID")] Speaker speaker)
{
if(ModelState.IsValid)
{
this.context.SpeakerDBSet.Add(speaker);
this.context.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.RoomID = new SelectList(this.context.RoomDBSet, "RoomID", "Name", speaker.RoomID);
return View(speaker);
}
#endregion
#region 편집 페이지 처리하기 - Edit(id) [GET : Speaker/Edit/5]
/// <summary>
/// 편집 페이지 처리하기
/// </summary>
/// <param name="id">ID</param>
/// <returns>액션 결과</returns>
public ActionResult Edit(int? id)
{
if(id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Speaker speaker = this.context.SpeakerDBSet.Find(id);
if(speaker == null)
{
return HttpNotFound();
}
ViewBag.RoomID = new SelectList(this.context.RoomDBSet, "RoomID", "Name", speaker.RoomID);
return View(speaker);
}
#endregion
#region 편집 페이지 처리하기 - Edit(speaker) [POST : Speaker/Edit/5]
/// <summary>
/// 편집 페이지 처리하기
/// </summary>
/// <param name="speaker">스피커</param>
/// <returns>액션 결과</returns>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "SpeakerID,Name,RoomID")] Speaker speaker)
{
if(ModelState.IsValid)
{
this.context.Entry(speaker).State = EntityState.Modified;
this.context.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.RoomID = new SelectList(this.context.RoomDBSet, "RoomID", "Name", speaker.RoomID);
return View(speaker);
}
#endregion
#region 삭제 페이지 처리하기 - Delete(id) [GET : Speaker/Delete/5]
/// <summary>
/// 삭제 페이지 처리하기
/// </summary>
/// <param name="id">ID</param>
/// <returns>액션 결과</returns>
public ActionResult Delete(int? id)
{
if(id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Speaker speaker = this.context.SpeakerDBSet.Find(id);
if(speaker == null)
{
return HttpNotFound();
}
return View(speaker);
}
#endregion
#region 확인시 삭제 페이지 처리하기 - DeleteConfirmed(id) [POST : Speaker/Delete/5]
/// <summary>
/// 확인시 삭제 페이지 처리하기
/// </summary>
/// <param name="id">ID</param>
/// <returns>액션 결과</returns>
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Speaker speaker = this.context.SpeakerDBSet.Find(id);
this.context.SpeakerDBSet.Remove(speaker);
this.context.SaveChanges();
return RedirectToAction("Index");
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Protected
#region 리소스 해제하기 - Dispose(disposing)
/// <summary>
/// 리소스 해제하기
/// </summary>
/// <param name="disposing">리소스 해제 여부</param>
protected override void Dispose(bool disposing)
{
if(disposing)
{
this.context.Dispose();
}
base.Dispose(disposing);
}
#endregion
}
}
▶ Room/Index.cshtml
@model IEnumerable<TestProject.Models.Room>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
@foreach(var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.RoomID }) |
@Html.ActionLink("Details", "Details", new { id = item.RoomID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.RoomID })
</td>
</tr>
}
</table>
▶ Room/Create.cshtml
@model TestProject.Models.Room
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using(Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Room</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes : new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts
{
@Scripts.Render("~/bundles/jqueryval")
}
▶ Room/Edit.cshtml
@model TestProject.Models.Room
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Room</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.RoomID)
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes : new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts
{
@Scripts.Render("~/bundles/jqueryval")
}
▶ Room/Details.cshtml
@model TestProject.Models.Room
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Room</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Name)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.RoomID }) |
@Html.ActionLink("Back to List", "Index")
</p>
▶ Room/Delete.cshtml
@model TestProject.Models.Room
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Room</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Name)
</dd>
</dl>
@using(Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
@Html.ActionLink("Back to List", "Index")
</div>
}
</div>
▶ Speaker/Index.cshtml
@model IEnumerable<TestProject.Models.Speaker>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Room.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
@foreach(var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Room.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.SpeakerID }) |
@Html.ActionLink("Details", "Details", new { id = item.SpeakerID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.SpeakerID })
</td>
</tr>
}
</table>
▶ Speaker/Create.cshtml
@model TestProject.Models.Speaker
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Speaker</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes : new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RoomID, "RoomID", htmlAttributes : new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("RoomID", null, htmlAttributes : new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.RoomID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts
{
@Scripts.Render("~/bundles/jqueryval")
}
▶ Speaker/Edit.cshtml
@model TestProject.Models.Speaker
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using(Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Speaker</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.SpeakerID)
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes : new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RoomID, "RoomID", htmlAttributes : new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("RoomID", null, htmlAttributes : new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.RoomID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts
{
@Scripts.Render("~/bundles/jqueryval")
}
▶ Speaker/Details.cshtml
@model TestProject.Models.Speaker
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Speaker</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Room.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Room.Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Name)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.SpeakerID }) |
@Html.ActionLink("Back to List", "Index")
</p>
▶ Speaker/Delete.cshtml
@model TestProject.Models.Speaker
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Speaker</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Room.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Room.Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Name)
</dd>
</dl>
@using(Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
@Html.ActionLink("Back to List", "Index")
</div>
}
</div>
728x90
그리드형(광고전용)