• orm框架中entityframework的 增删改查操作


    /////////////////////第一种//////////////////////////

    namespace ConsoleApplication3

    {

        class Program

        {

            static void Main(string[] args)

            {

                Model1Container m = new Model1Container();

                Student s = new Student();

                s.Name = "小明";

                m.Student集.AddObject(s);   //增加操作

                m.SaveChanges();            //运行增加

                Console.WriteLine("增加成功");

                var Slist = from Stu in m.Student集 select Stu;   //查询操作

                foreach (Student sl in Slist)

                {

                    Console.WriteLine("输出的学生名字为:");

                    Console.WriteLine(sl.Name);

                }

                Console.WriteLine("更新操作");

                Student sc = new Student();

                sc.ID = 2;

                sc.Name = "小改明";

                m.Student集.Attach(sc);   //这句很重要把要更新的实体写入实体集中

                m.ObjectStateManager.ChangeObjectState(sc, System.Data.EntityState.Modified);   //对实体进行更新

                m.SaveChanges();   //运行更新操作

                Console.WriteLine("删除操作");

                Student sd = new Student();

                sd.ID = 3;

                m.Student集.Attach(sd);

                m.ObjectStateManager.ChangeObjectState(sd, System.Data.EntityState.Deleted);  //删除操作

                m.SaveChanges();

            }

        }

    }

    ////////////////////////////////////第二种//////////////////////////

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ConsoleApplication1

    {

        class Program

        {

            static void Main(string[] args)

            {

                //创建数据库实体

                ModelStudentContainer DemoDB = new ModelStudentContainer();

                #region 增加数据

                ////创建具体实体(表中一行)

                //Student stu1 = new Student()

                //{

                //    ID = 1,

                //    Name = "张飞",

                //    Age = 20

                //};

                //Student stu2 = new Student()

                //{

                //    ID = 2,

                //    Name = "关羽",

                //    Age = 21

                //};

                //Student stu3 = new Student()

                //{

                //    ID = 3,

                //    Name = "赵云",

                //    Age = 22

                //};

                //Student stu4 = new Student()

                //{

                //    ID = 4,

                //    Name = "刘备",

                //    Age = 23

                //};

                //Student stu5 = new Student()

                //{

                //    ID = 5,

                //    Name = "刘表",

                //    Age = 24

                //};

                //Student stu6 = new Student()

                //{

                //    ID = 6,

                //    Name = "刘禅",

                //    Age = 25

                //};

                ////将实体stu添加到数据库实体对象中

                //DemoDB.Student.AddObject(stu1);

                //DemoDB.Student.AddObject(stu2);

                //DemoDB.Student.AddObject(stu3);

                //DemoDB.Student.AddObject(stu4);

                //DemoDB.Student.AddObject(stu5);

                //DemoDB.Student.AddObject(stu6);

                ////保存对数据库的修改

                //DemoDB.SaveChanges();

                #endregion

                #region 查询1:一般查询,查询所有数据

                //var result = from u in DemoDB.Student

                //             select u;

                //foreach (var item in result)

                //{

                //    Console.WriteLine("ID:{0},姓名:{1},年龄:{2}。", item.ID, item.Name, item.Age);

                //}

                #endregion

                #region 查询2:使用ToList防止延迟加载,拆分子查询

                ////这个查询结果可以保存到服务器内存中

                //var result1 = (from u in DemoDB.Student

                //               where u.Age >= 22

                //               select u).ToList<Student>();

                //var result2 = from u in result1

                //              where u.Name != "赵云"

                //              select u;

                //foreach (var item in result2)

                //{

                //    Console.WriteLine("ID:{0},姓名:{1},年龄:{2}。", item.ID, item.Name, item.Age);

                //}

                #endregion

                #region 查询3:使用Where方法(lambda表达式)

                ////使用了Where泛型方法,要传入一个lambda表达式

                //var result = DemoDB.Student

                //    .Where<Student>(t => t.ID == 1);

                //foreach (var item in result)

                //{

                //    Console.WriteLine("ID:{0},姓名:{1},年龄:{2}。", item.ID, item.Name, item.Age);

                //}

                #endregion

                #region 查询4:Linq分页查询

                ////要跳过多少页

                //int skipPage;

                ////每页两条数据

                //int countPerPage=2;

                ////从跳过0页到跳过2页(输出第1-3页)

                //for (skipPage = 0; skipPage <= 2; skipPage++)

                //{

                //    var result = DemoDB.Student

                ////分页必须排序

                //        .OrderBy(t => t.ID)

                ////跳过指定页数

                //        .Skip<Student>(skipPage * countPerPage)

                ////获取条数

                //        .Take<Student>(countPerPage);

                //    Console.WriteLine("第{0}页:", skipPage + 1);

                ////输出查询结果(该页)

                //    foreach (var item in result)

                //    {

                //        Console.WriteLine("ID:{0},姓名:{1},年龄:{2}。", item.ID, item.Name, item.Age);

                //    }

                //    Console.WriteLine();

                //}

                #endregion

                #region 查询5:使用匿名类查询多个字段

                //var result = from p in DemoDB.Student

                //             where p.ID < 4

                ////定义一个包含了ID和Name信息的匿名类

                //             select new { p.ID, p.Name };

                //foreach (var item in result)

                //{

                ////这里无法输出年龄

                //    Console.WriteLine("ID:{0},姓名:{1}。", item.ID, item.Name);

                //}

                #endregion

                #region 修改1:修改1条记录

                ////这里没有写where字句,会查询出4条记录

                //var result = from u in DemoDB.Student

                //             select u;

                ////但是这个方法一次只能取出一条记录

                ////注:First方法如果返回空值则抛异常

                //var target = result.FirstOrDefault<Student>();

                ////所以只修改了查询结果集中的第一条记录

                //target.Name = "司马懿";

                //DemoDB.SaveChanges();

                #endregion

                #region 修改2:修改多条记录

                //var result = from u in DemoDB.Student

                //             select u;

                ////使用循环方式,修改所有查询到的记录

                //foreach (var item in result)

                //{

                //    item.Age = 10;

                //}

                //DemoDB.SaveChanges();

                #endregion

                #region 删除

                //var result = from u in DemoDB.Student

                //             where u.Name == "关羽"

                //             select u;

                ////删除所有查询结果(这里只有一条结果)

                //foreach (var item in result)

                //{

                //    DemoDB.Student.DeleteObject(item);

                //}

                //DemoDB.SaveChanges();

                #endregion

                Console.WriteLine("执行完毕!");

                Console.ReadKey();

            }

        }

    }

  • 相关阅读:
    Resource Path Location Type Target runtime Apache Tomcat v6.0 is not defined已解决
    项目跳转白名单防止其他系统随意跳转到本系统过滤器实现
    页面跳转的方式Redirect跳转机制:
    总结一下在ASP.NET中开发网站的一般步骤
    总结asp控件的重要属性
    页面跳转
    ASP.NET中的Image和ImageButton控件
    页面跳转的方式
    总结基本概念
    总结asp标准控件的重要属性
  • 原文地址:https://www.cnblogs.com/mengqimoli/p/8552520.html
Copyright © 2020-2023  润新知