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