Entity Framework elementary Article 7-basic operations: add, update, delete, and transaction

Source: Internet
Author: User

In this sectionCodeTo learn. These basic operations are relatively simple. The content related to these basic operations is described in sections 1 to 6.

LAdded:

Method 1: UseAddtoxxx (XXX)Method: The instance code is as follows:

Using(VaREDM =New Northwindentities())

{

MERsC =New MERs{Customerid ="C #", City =" Chengdu" , Address =" Sichuan, China" , CompanyName ="Cnblogs", Country =" China" , Fax ="10086", Phone ="1008611", Postalcode ="610000", Region =" Tianfu Square" , Contactname =" Windmill. Net" };

EDM. addtocustomers (C );

IntResult = EDM. savechanges ();

Assert. Areequal (result, 1 );

MERsADDC = EDM. Customers. firstordefault (Cc => cc. customerid ="C #");

Console. Writeline ("Customerid = {0}, city = {1 }", ADDC. customerid, ADDC. City );

}

Method 2: UseObjectcontextOfAddobject (String entitysetname,ObjectEntity)Method. The instance code is as follows:

Using(VaREDM =New Northwindentities())

{

MERsC =New MERs{Customerid ="C2", City =" Chengdu 2" , Address =" Sichuan, China 2" , CompanyName ="Cnblogs", Country =" China" , Fax ="10086", Phone ="1008611", Postalcode ="610000", Region =" Tianfu Square" , Contactname =" Windmill. Net" };

EDM. addobject ("MERs", C );

IntResult = EDM. savechanges ();

Assert. Areequal (result, 1 );

MERsADDC = EDM. Customers. firstordefault (Cc => cc. customerid ="C2");

Console. Writeline ("Customerid = {0}, city = {1 }", ADDC. customerid, ADDC. City );

}

In the code, note that the parameter "entitysetname" in the addobject method refers to the corresponding object name, which should be:"MERs", Not"Northwindentities. MERs";

LUpdate:

Using(VaREDM =New Northwindentities())

{

MERsADDC = EDM. Customers. firstordefault (Cc => cc. customerid ="C2");

ADDC. City ="Cd";

ADDC. contactname ="Cnblogs";

ADDC. Country ="Cn";

IntResult = EDM. savechanges ();

Assert. Areequal (result, 1 );

MERsUpdatec = EDM. Customers. firstordefault (Cc => cc. customerid ="C2");

Console. Writeline ("Customerid = {0}, city = {1 }", Updatec. customerid, updatec. City );

}

Note that the primary key cannot be updated; otherwise,"System. invalidoperationexception:The property "XXX" is part of the key information of the object and cannot be modified."

LDelete:

The instance code is as follows:

Using(VaREDM =New Northwindentities())

{

MERsDeletec = EDM. Customers. firstordefault (Cc => cc. customerid ="C2");

EDM. deleteobject (deletec );

IntResult = EDM. savechanges ();

Assert. Areequal (result, 1 );

MERsC = EDM. Customers. firstordefault (Cc => cc. customerid ="C2");

Assert. Areequal (C,Null);

}

LTransaction:

The instance code is as follows:

NorthwindentitiesEDM =Null;

System. Data. Common.DbtransactionTRAN =Null;

Try

{

EDM =New Northwindentities();

EDM. Connection. open ();

TRAN = EDM. Connection. begintransaction ();

MERsCST = EDM. Customers. firstordefault (Cc => cc. customerid ="C #");

CST. Country ="Cn";

CST. City ="Cd";

EDM. savechanges ();

Tran. Commit ();

}

Catch(ExceptionEx)

{

If(Tran! =Null)

Tran. rollback ();

ThrowEx;

}

Finally

{

If(EDM! =Null& EDM. Connection. State! = System. Data.Connectionstate. Closed)

EDM. Connection. Close ();

}

now, the basic introduction is complete. Later, I plan to write some intermediate articles.

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.