博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
精进不休 .NET 4.0 (8) - ADO.NET Entity Framework 4.0 Self Tracking Entity
阅读量:5996 次
发布时间:2019-06-20

本文共 6030 字,大约阅读时间需要 20 分钟。


 



精进不休 .NET 4.0 (8) - ADO.NET Entity Framework 4.0 Self Tracking Entity


作者:



介绍

ADO.NET Entity Framework 4.0 的新增功能

  • 对 Self Tracking Entity(实体状态自跟踪)的支持,基于 POCO 
  • WCF 结合 Self Tracking Entity 的应用 
示例
1、Self Tracking Entity 的 Demo
SelfTrackingDemo/BLL.cs
/* 

* ADO.NET Entity Framework 4.0 - 对 Self Tracking Entity(实体状态自跟踪)的支持,基于 POCO 

*         1、不通过 WCF 使用 Self Tracking Entity 需要手动调用 StartTracking() 

*         2、MarkAsAdded(), MarkAsModified(), MarkAsDeleted() 会自动 StartTracking() 

*         3、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

                } 

        } 

}
 
 
 
 
OK 
 
     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/341389
,如需转载请自行联系原作者
你可能感兴趣的文章
ceph存储 磁盘IOPS常识
查看>>
ORA-12720: operation requires database is in EXCLUSIVE mode
查看>>
ELK日志服务使用-kafka传输日志(bbotte.com)
查看>>
linux系统之iptables其二命令注解
查看>>
Silverlight C# 游戏开发:高深莫测却浅显易懂的游戏开发
查看>>
标准ACL+扩展ACL+命名ACL
查看>>
Linux常用的基本命令14
查看>>
《zabbix进程组成结构与zabbix_agentd.conf配置文件参数详解》-3
查看>>
8-22学习练习[一个viewController整合增删移动功能]
查看>>
MySQL的字符集
查看>>
Selenium2+python自动化63-简易项目搭建
查看>>
Managed Debugging Assistant &#39;PInvokeStackImbalance&#39; has detected a problem in 解决方案
查看>>
centos7 安装mysql5.7.11注意事项
查看>>
[20150727]''与NULL.txt
查看>>
上海往事之教会宝宝学游泳
查看>>
SharePoint 2013 图文开发系列之创建内容类型
查看>>
cookie 简介
查看>>
ios和android内嵌h5页面联调小结
查看>>
两种jdk版本的多个tomcat按windows服务的安装问题的解决
查看>>
为IE创建更好的XMLHttpRequest对象
查看>>