• LinqToSql增加、修改、删除---(转载)


    LinqToSql ,EF:外键实体的CRD  

    2011-03-06 21:56:26|  分类: linq |  标签:编程  |字号 订阅

     
     
          接触LinqTosql是很久的事情了,之前一直在拼接字符串,所以一开始就很喜欢这种ORM的方式,强类型用的爽啊,最近开始

    改为学习ADO.NET 实体框架了:),概念上的东西网上都很多,当我想实现一个简单的外键表操作时,却没有一个较好的例子,

    经过一段摸索后,将最简单的情形给贴出来。

         本文用这2种方式通过一个简单的外键关系来演示Insert,Update,Delete操作

         DB中建立2张表,这里只考虑1对多的关系:

    image

    LinqToSql的方式:

          生成的TestDB.dbml:

    image

    Insert:

            using(TestDBDataContext db=new TestDBDataContext())  
            {  
                Depatment depatment = new Depatment() {DeptName = "开发部"};  
                Employee employee = new Employee()  
                                        {  
                                            EmpName = "Ringgo.Yao",  
                                            Depatment = depatment  
                                        };  
                db.Depatment.InsertOnSubmit(depatment);  
                db.SubmitChanges();  
            }

         当提交Department时,会自动提交Employee      

        Update:

            using (TestDBDataContext db = new TestDBDataContext())  
            {  
                Depatment depatment = db.Depatment.Where(i => i.DeptId ==2).First();  
                depatment.DeptName = "研发部";  
                Employee employee = new Employee()  
                {  
                    EmpName = "Ringgo.Yao",  
                    Depatment = depatment  
                };              
                db.SubmitChanges();  
            }

           这里修改Department记录的同时添加了一条Employee记录

         Delete:

           using (TestDBDataContext db = new TestDBDataContext())  
            {  
              Depatment depatment=db.Depatment.Where(i => i.DeptId == 3).First();  
              var employees=db.Employee.Where(i => i.DeptId == 3).ToList();    
              db.Employee.DeleteAllOnSubmit(employees);  
              db.Depatment.DeleteOnSubmit(depatment);  
              db.SubmitChanges();  
            }

            删除的时候要先删除Employee,再删除Department

         ADO.NET Entity Framework的方式:

          生成的TestDB.edmx:

    image

      因为代码看起来似乎区别不大,所以就不进行说明了

         Insert:

            using(TestDBEntities db=new TestDBEntities())  
            {  
                Depatment depatment = new Depatment() {DeptName = "开发部"};  
                Employee employee = new Employee() {EmpName = "Ringgo.Yao"};  
                depatment.Employee.Add(employee);  
                db.Depatment.AddObject(depatment);  
                db.SaveChanges();  
            }

        

         Update:

            using (TestDBEntities db = new TestDBEntities())  
            {  
                var dept = db.Depatment.Where(i => i.DeptId == 1).First();  
                dept.DeptName = "研发部";  
                Employee employee = new Employee() {EmpName = "White"};  
                dept.Employee.Add(employee);  
                db.SaveChanges();  
            }
      Delete:
           using (TestDBEntities db = new TestDBEntities())  
            {  
                var dept = db.Depatment.Where(i => i.DeptId == 1).First();  
                foreach (var emp in dept.Employee.ToList())  
                {                  
                    dept.Employee.Remove(emp);  
                    db.Employee.DeleteObject(emp);  
                }  
                db.Depatment.DeleteObject(dept);  
                db.SaveChanges();  
            }
  • 相关阅读:
    VTK 图像统计_彩色直方图计算
    VTK 图像统计_灰度直方图计算
    VTK 图像基本操作_三维图像切片交互提取(回调函数、观察者-命令模式)
    VTK 图像基本操作_三维图像切片提取
    VTK 图像基本操作_图像子块提取(特征区域提取)
    VTK 图像基本操作_单颜色通道图像合成彩色
    VTK 图像基本操作_灰度图像映射成伪彩色图像(查表法)
    VTK 图像基本操作_彩色图像成分提取
    VTK 图像基本操作_彩色图像生成灰度图像
    VTK 图像基本操作_图像类型转换
  • 原文地址:https://www.cnblogs.com/honghong75042/p/3402281.html
Copyright © 2020-2023  润新知