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.