Ado.net Entity Framework 4.0 Self tracking Entity
Introduced
New features of the Ado.net Entity Framework 4.0
* Support for self tracking Entity (entity State self-tracking), based on POCO
* WCF combined Self tracking Entity application
Example
1, Self tracking Entity Demo
Selftrackingdemo/bll.cs
Code
/*
* Ado.net Entity Framework 4.0-support for self tracking Entity (entity State self-tracking), based on POCO
* 1, do not use the Self tracking through WCF Entity need to manually invoke Starttracking ()
* 2, markasadded (), markasmodified (), markasdeleted () will automatically starttracking ()
* 3, ApplyChanges () The role is: binding entities to the context, through changeobjectstate change the state of the entity, through the changerelationshipstate change the state of the associated entity
*
* This demo demonstrates how to implement Tanku additions and deletions through Self tracking Entity
* If related entities are involved, you can refer to the "Foreign key support" Demo in http://www.cnblogs.com/webabcd/archive/2010/06/17/1759314.html
*/
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Namespace Selftrackingdemo
{
public class BLL
{
Collect entity collections for all product categories
Public list<productcategory> getcategories ()
{
using (selftrackingentities CTX = new Selftrackingentities ())
{
var result = ctx. Productcategories.tolist ();
return result;
}
}
Take product category entities by ID
Public ProductCategory getcategory (int CategoryID)
{
using (selftrackingentities CTX = new Selftrackingentities ())
{
var result = ctx. Productcategories.single (c => C.productcategoryid = = CategoryID);
return result;
}
}
Update related data in the database based on product category entity
public void Updatecategory (ProductCategory category)
{
using (selftrackingentities CTX = new Selftrackingentities ())
{
The internal logic of ApplyChanges () is to bind the entity to the context, to change the state of the entity through the changeobjectstate, and to change the state of the associated entity by changerelationshipstate
CTx. Productcategories.applychanges (category);
Implement add, update, delete operations to entities based on the state of the entity
var affectedrow = ctx. SaveChanges ();
}
}
Delete related data from a database based on the product category entity
public void Deletecategory (ProductCategory category)
{
Mark the entity as a delete state
Category. Markasdeleted ();
Updatecategory (category);
}
Add new data to the database based on the product category entity
public void Addcategory (ProductCategory category)
{
Updatecategory (category);
}
}
}