아래 코드는
ADO.NET Entity Framework를 사용하여 만든 개체에 접근해서
데이터를 출력, 입력, 수정, 삭제하는 샘플 코드를 기록해 본 내용입니다.
FrmEntityFramework.aspx
===================================================================
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="FrmEntityFramework.aspx.cs" Inherits="WebEntityFramework.FrmEntityFramework" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>엔터티 프레임워크를 사용한 입력, 출력, 수정, 삭제</h2>
<div>
카테고리 :
<asp:DropDownList ID="lstCategory" runat="server">
</asp:DropDownList>
상품명 :
<asp:TextBox ID="txtProductName" runat="server"></asp:TextBox>
상품가격 :
<asp:TextBox ID="txtUnitPrice" runat="server"></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="상품입력" onclick="btnAdd_Click" />
<asp:Button ID="btnUpdate" runat="server" Text="상품수정"
onclick="btnUpdate_Click" />
<asp:Button ID="btnDelete" runat="server" Text="상품삭제"
onclick="btnDelete_Click" />
</div>
<asp:GridView ID="ctlProductLists" runat="server">
</asp:GridView>
</asp:Content>
FrmEntityFramework.aspx.cs
===================================================================
using System;
using System.Linq;
using System.Web.UI;
using WebEntityFramework.Models; //
namespace WebEntityFramework
{
public partial class FrmEntityFramework : System.Web.UI.Page
{
private Models.NorthwindEntities context = new Models.NorthwindEntities();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DisplayData();
DisplayCategory();
}
}
private void DisplayCategory()
{
// 카테고리 바인딩
var q = from c in context.Categories
orderby c.CategoryID descending
select new { CategoryID = c.CategoryID, CategoryName = c.CategoryName };
lstCategory.DataSource = q.ToList();
lstCategory.DataTextField = "CategoryName";
lstCategory.DataValueField = "CategoryID";
lstCategory.DataBind();
}
private void DisplayData()
{
// LINQ를 사용한 출력
var q = context.Products.OrderByDescending(p => p.ProductID);
this.ctlProductLists.DataSource = q.ToList();
this.ctlProductLists.DataBind();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
// 엔터티 개체 생성
Models.Product p = new Product();
// 각각의 속성 지정
p.CategoryID = Convert.ToInt32( lstCategory.SelectedValue );
p.ProductName = txtProductName.Text;
p.UnitPrice = Convert.ToDecimal( txtUnitPrice.Text );
// AddObject()
context.Products.AddObject(p); // Add, Delete,
// SaveChanges()
context.SaveChanges(); // 실행
DisplayData();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
// 79번 개체 가져오기
Models.Product p = context.Products.Where(pro => pro.ProductID == 79).SingleOrDefault();
// 원하는 값 수정
p.ProductName = "좋은 컴퓨터로 변경";
// 실행/적용
context.SaveChanges();
DisplayData();
}
protected void btnDelete_Click(object sender, EventArgs e)
{
// 79번 개체 삭제
Models.Product p = context.Products.First(pro => pro.ProductID == 79);
if (p != null)
{
context.Products.DeleteObject(p); //
context.SaveChanges(); //
}
DisplayData();
}
}
}
끝.