Entity Framework Learning Primary 7--Basic operations: Add, UPDATE, delete, transaction

Source: Internet
Author: User

This section, write directly through the code to learn. These basic operations are simpler, and the content associated with these basic operations is basically covered in the previous sections 1 through 6.

L Increase:

Method 1: Use the addtoxxx (XXX) method: the instance code is as follows:

using (var edm = new NorthwindEntities())

{

Customers c = new Customers { CustomerID = "c#", City = "成都市", Address = "中国四川省", CompanyName = "cnblogs", Country = "中国", Fax = "10086", Phone = "1008611", PostalCode = "610000", Region = "天府广场", ContactName = "风车车.Net" };

edm.AddToCustomers(c);

int result = edm.SaveChanges();

Assert.AreEqual (result, 1);

Customers addc = edm.Customers.FirstOrDefault(cc => cc.CustomerID == "c#");

Console.WriteLine("CustomerId={0},City={1}", addc.CustomerID, addc.City);

}

Method 2: Use the ObjectContext addobject (string entitySetName, Object entity) method. The instance code is as follows:

using (var edm = new NorthwindEntities())

{

Customers c = new Customers { CustomerID = "c2", City = "成都市2", Address = "中国四川省2", CompanyName = "cnblogs", Country = "中国", Fax = "10086", Phone = "1008611", PostalCode = "610000", Region = "天府广场", ContactName = "风车车.Net" };

edm.AddObject("Customers", c);

int result = edm.SaveChanges();

Assert.AreEqual (result, 1);

Customers addc = edm.Customers.FirstOrDefault(cc => cc.CustomerID == "c2");

Console.WriteLine("CustomerId={0},City={1}", addc.CustomerID, addc.City);

}

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.