[索引頁]
[×××]
精進不休 .NET 4.0 (8) - ADO.NET Entity Framework 4.0 Self Tracking Entity
做者:
webabcd
介紹
ADO.NET Entity Framework 4.0 的新增功能
- 對 Self Tracking Entity(實體狀態自跟蹤)的支持,基於 POCO
- WCF 結合 Self Tracking Entity 的應用
示例
一、Self Tracking Entity 的 Demo
SelfTrackingDemo/BLL.cs
/*
* ADO.NET Entity Framework 4.0 - 對 Self Tracking Entity(實體狀態自跟蹤)的支持,基於 POCO
* 一、不經過 WCF 使用 Self Tracking Entity 須要手動調用 StartTracking()
* 二、MarkAsAdded(), MarkAsModified(), MarkAsDeleted() 會自動 StartTracking()
* 三、ApplyChanges() 的做用是:綁定實體到上下文,經過 ChangeObjectState 改變實體的狀態,經過 ChangeRelationshipState 改變關聯實體的狀態
*
* 本 Demo 演示如何經過 Self Tracking Entity 來實現對單表的增刪改查
* 若是涉及到關聯實體,能夠參考 http://webabcd.blog.51cto.com/1787395/341378 中的「對外鍵的支持」的 Demo
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SelfTrackingDemo
{
public
class BLL
{
// 取所有產品類別的實體集合
List<ProductCategory> GetCategories() List<ProductCategory> GetCategories()
{
using (SelfTrackingEntities ctx =
new SelfTrackingEntities())
{
var result = ctx.ProductCategories.ToList();
return result;
}
}
// 根據 id 取產品類別實體
ProductCategory GetCategory() ProductCategory GetCategory(int categoryId)
{
using (SelfTrackingEntities ctx =
new SelfTrackingEntities())
{
var result = ctx.ProductCategories.Single(c => c.ProductCategoryID == categoryId);
return result;
}
}
// 根據產品類別實體更新數據庫中的相關數據
void UpdateCategory() void UpdateCategory(ProductCategory category)
{
using (SelfTrackingEntities ctx =
new SelfTrackingEntities())
{
// ApplyChanges() 的內部邏輯爲:綁定實體到上下文,經過 ChangeObjectState 改變實體的狀態,經過 ChangeRelationshipState 改變關聯實體的狀態
ctx.ProductCategories.ApplyChanges(category);
// 根據實體的狀態,實現對實體的 添加、更新、刪除 操做
var affectedRow = ctx.SaveChanges();
}
}
// 根據產品類別實體刪除數據庫中的相關數據
void DeleteCategory() void DeleteCategory(ProductCategory category)
{
// 標記該實體爲刪除狀態
category.MarkAsDeleted();
UpdateCategory(category);
}
// 根據產品類別實體向數據庫添加新的數據
void AddCategory() void AddCategory(ProductCategory category)
{
UpdateCategory(category);
}
}
}
SelfTrackingDemo/Demo.aspx
<%@ Page Language=
"C#" AutoEventWireup=
"true" CodeBehind=
"Demo.aspx.cs"
Inherits=
"SelfTrackingDemo.Demo" %>
<!DOCTYPE html
PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=
"http://www.w3.org/1999/xhtml">
<head runat=
"server">
<title></title>
</head>
<body>
<form id=
"form1" runat=
"server">
<div>
<asp:ListView ID=
"ListView1" runat=
"server" DataSourceID=
"ObjectDataSource1" DataKeyNames=
"ProductCategoryID"
InsertItemPosition=
"LastItem" OnItemUpdating=
"ListView1_ItemUpdating">
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID=
"UpdateButton" runat=
"server" CommandName=
"Update" Text=
"Update" />
<asp:Button ID=
"CancelButton" runat=
"server" CommandName=
"Cancel" Text=
"Cancel" />
</td>
<td>
</td>
<td>
</td>
<td>
<asp:TextBox ID=
"NameTextBox" runat=
"server" Text=
'<%# Bind("Name") %>' />
</td>
<td>
</td>
<td>
</td>
</tr>
</EditItemTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID=
"InsertButton" runat=
"server" CommandName=
"Insert" Text=
"Insert" />
<asp:Button ID=
"CancelButton" runat=
"server" CommandName=
"Cancel" Text=
"Clear" />
</td>
<td>
</td>
<td>
</td>
<td>
<asp:TextBox ID=
"NameTextBox" runat=
"server" Text=
'<%# Bind("Name") %>' />
</td>
<td>
</td>
<td>
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Button ID=
"DeleteButton" runat=
"server" CommandName=
"Delete" Text=
"Delete" />
<asp:Button ID=
"EditButton" runat=
"server" CommandName=
"Edit" Text=
"Edit" />
</td>
<td>
<asp:Label ID=
"ProductCategoryIDLabel" runat=
"server" Text=
'<%# Eval("ProductCategoryID") %>' />
</td>
<td>
<asp:Label ID=
"ParentProductCategoryIDLabel" runat=
"server" Text=
'<%# Eval("ParentProductCategoryID") %>' />
</td>
<td>
<asp:Label ID=
"NameLabel" runat=
"server" Text=
'<%# Eval("Name") %>' />
</td>
<td>
<asp:Label ID=
"rowguidLabel" runat=
"server" Text=
'<%# Eval("rowguid") %>' />
</td>
<td>
<asp:Label ID=
"ModifiedDateLabel" runat=
"server" Text=
'<%# Eval("ModifiedDate") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id=
"itemPlaceholderContainer" runat=
"server" border=
"0" style="">
<tr runat=
"server" style="">
<th runat=
"server">
</th>
<th runat=
"server">
ProductCategoryID
</th>
<th runat=
"server">
ParentProductCategoryID
</th>
<th runat=
"server">
Name
</th>
<th runat=
"server">
rowguid
</th>
<th runat=
"server">
ModifiedDate
</th>
</tr>
<tr id=
"itemPlaceholder" runat=
"server">
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
<asp:ObjectDataSource ID=
"ObjectDataSource1" runat=
"server" DataObjectTypeName=
"SelfTrackingDemo.ProductCategory"
DeleteMethod=
"DeleteCategory" InsertMethod=
"AddCategory" SelectMethod=
"GetCategories"
TypeName=
"SelfTrackingDemo.BLL" OnInserting=
"ObjectDataSource1_Inserting" OnDeleting=
"ObjectDataSource1_Deleting">
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>
SelfTrackingDemo/Demo.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SelfTrackingDemo
{
public partial
class Demo : System.Web.UI.Page
{
void Page_Load() void Page_Load(object sender, EventArgs e)
{
}
void ObjectDataSource1_Inserting() void ObjectDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
var category = e.InputParameters[0]
as ProductCategory;
category.rowguid = Guid.NewGuid();
category.ModifiedDate = DateTime.Now;
}
void ObjectDataSource1_Deleting() void ObjectDataSource1_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
{
}
void ListView1_ItemUpdating() void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
BLL bll =
new BLL();
var category = bll.GetCategory((int)ListView1.DataKeys[e.ItemIndex].Value);
// 注意:這裏必定要手動調用 StartTracking() 方法,用於跟蹤實體狀態的改變
category.StartTracking();
category.Name = e.NewValues[
"Name"].ToString();
bll.UpdateCategory(category);
ListView1.EditIndex = -1;
e.Cancel =
true;
}
}
}