Diligence. NET 4.0 (8)

Source: Internet
Author: User

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);
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.