ADO.NET Entity Framework 学习初级篇7--基本操作:增加、更新、删除、事务

本节,直接写通过代码来学习。这些基本操作都比较简单,与这些基本操作相关的内容在之前的1至6节基本介绍完毕。

l          增加:

方法1:使用AddToXXX(xxx)方法:实例代码如下:

  1.             using (var edm = new NorthwindEntities())
  2.             {
  3.                 Customers c = new Customers { CustomerID = "c#", City = "成都市", Address = "中国四川省", CompanyName = "cnblogs", Country = "中国", Fax = "10086", Phone = "1008611", PostalCode = "610000", Region = "天府广场", ContactName = "风车车.Net" };
  4.                 edm.AddToCustomers(c);
  5.                 int result = edm.SaveChanges();
  6.                 Assert.AreEqual(result, 1);
  7.                 Customers addc = edm.Customers.FirstOrDefault(cc => cc.CustomerID == "c#");
  8.                 Console.WriteLine("CustomerId={0},City={1}", addc.CustomerID, addc.City);
  9.             }
复制代码

方法2:使用ObjectContext的AddObject(string entitySetName, object entity)方法。实例代码如下:

  1. using (var edm = new NorthwindEntities())
  2.             {
  3.                     Customers c = new Customers { CustomerID = "c2", City = "成都市2", Address = "中国四川省2", CompanyName = "cnblogs", Country = "中国", Fax = "10086", Phone = "1008611", PostalCode = "610000", Region = "天府广场", ContactName = "风车车.Net" };
  4.                     edm.AddObject("Customers", c);
  5.                     int result = edm.SaveChanges();
  6.                     Assert.AreEqual(result, 1);
  7.                     Customers addc = edm.Customers.FirstOrDefault(cc => cc.CustomerID == "c2");
  8.                     Console.WriteLine("CustomerId={0},City={1}", addc.CustomerID, addc.City);
  9.             }
复制代码

其中,在代码中,需要注意的是:AddObject方法中参数“entitySetName ”就是指对应实体名称,应该是:“Customers”,而不是“NorthwindEntities.Customers”;

l          更新:

  1. using (var edm = new NorthwindEntities())
  2.             {
  3.                     Customers addc = edm.Customers.FirstOrDefault(cc => cc.CustomerID == "c2");
  4.                     addc.City = "CD";
  5.                     addc.ContactName = "cnblogs";
  6.                     addc.Country = "CN";
  7.                     int result = edm.SaveChanges();
  8.                     Assert.AreEqual(result, 1);
  9.                     Customers updatec = edm.Customers.FirstOrDefault(cc => cc.CustomerID == "c2");
  10.                     Console.WriteLine("CustomerId={0},City={1}", updatec.CustomerID, updatec.City);
  11.               }
复制代码

其中,需要注意的是:不能去更新主键,否则会报“System.InvalidOperationException : 属性“xxx”是对象的键信息的一部分,不能修改。”

l          删除:

实例代码如下:

  1. using (var edm = new NorthwindEntities())
  2.         {
  3.                     Customers deletec = edm.Customers.FirstOrDefault(cc => cc.CustomerID == "c2");
  4.                     edm.DeleteObject(deletec);
  5.                     int result = edm.SaveChanges();
  6.                     Assert.AreEqual(result, 1);
  7.                     Customers c = edm.Customers.FirstOrDefault(cc => cc.CustomerID == "c2");
  8.                     Assert.AreEqual(c, null);
  9.                
  10.         }
复制代码

l          事务

实例代码如下:

  1. NorthwindEntities edm = null;
  2.             System.Data.Common.DbTransaction tran = null;
  3.             try
  4.             {
  5.                 edm = new NorthwindEntities();
  6.                 edm.Connection.Open();
  7.                 tran = edm.Connection.BeginTransaction();
  8.                 Customers cst = edm.Customers.FirstOrDefault(cc => cc.CustomerID == "c#");
  9.                 cst.Country = "CN";
  10.                 cst.City = "CD";
  11.                 edm.SaveChanges();
  12.                 tran.Commit();
  13.             }
  14.             catch (Exception ex)
  15.             {
  16.                 if (tran != null)
  17.                     tran.Rollback();
  18.                 throw ex;
  19.             }
  20.             finally
  21.             {
  22.                 if (edm != null && edm.Connection.State != System.Data.ConnectionState.Closed)
  23.                     edm.Connection.Close();
  24.             }
复制代码

至此,初级篇基本介绍完毕。后面,打算写点,中级篇的东西。(文/♂风车车.Net

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值