• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
尜如
程序员 、 程序猿、 程序媛、我是程序媛媛媛
博客园    首页    新随笔    联系   管理    订阅  订阅
Entity Framework 学习初级篇7--基本操作:增加、更新、删除、事务

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

·           增加:

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

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

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

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

}

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

·           更新:

using (var edm = new NorthwindEntities())

{

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

      addc.City = "CD";

      addc.ContactName = "cnblogs";

      addc.Country = "CN";

      int result = edm.SaveChanges();

      Assert.AreEqual(result, 1);

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

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

}

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

·           删除:

实例代码如下:

using (var edm = new NorthwindEntities())

{

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

           edm.DeleteObject(deletec);

           int result = edm.SaveChanges();

           Assert.AreEqual(result, 1);

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

           Assert.AreEqual(c, null);

}

·          事务:

实例代码如下:

NorthwindEntities edm = null;

System.Data.Common.DbTransaction tran = null;

try

{

       edm = new NorthwindEntities();

       edm.Connection.Open();

       tran = edm.Connection.BeginTransaction();

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

       cst.Country = "CN";

       cst.City = "CD";

       edm.SaveChanges();

       tran.Commit();

}

catch (Exception ex)

{

       if (tran != null)

            tran.Rollback();

   throw ex;

}

finally

{

       if (edm != null && edm.Connection.State != System.Data.ConnectionState.Closed)

             edm.Connection.Close();

}

至此,初级篇基本介绍完毕。后面,打算写点,中级篇的东西。 

     本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
转自:http://www.cnblogs.com/xray2005/archive/2009/05/17/1458568.html
posted on 2012-10-24 15:52  尜如  阅读(281)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3