注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

天涯倦客的博客

祝福你朋友永远快乐!

 
 
 

日志

 
 

Entity Framework中增、删、改基本操作  

2013-03-09 21:18:38|  分类: asp.net |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

l、增加:

方法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”;

2、更新:

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”是对象的键信息的一部分,不能修改。”

3、删除:

实例代码如下:

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

                

         }

4、事务:

实例代码如下:

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

            }

  评论这张
 
阅读(519)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017