设为首页 加入收藏

TOP

使用ADO.NET实体数据模型
2019-10-09 19:59:05 】 浏览:77
Tags:使用 ADO.NET 实体 数据 模型

前景:要操作的数据表必须添加主键(方式:进入数据库-->数据表名-->设计-->列名右键-->设置主键)

 

 可在服务器资源管理器中查看是否设置了主键(主键会有一把钥匙的图样)

1)、项目名右键-->新建项-->ADO.NET数据模型

  

 

 

   选择第一个“来自数据库的EF设计器”就行

  

 

 

 如果是第一次连接,点击新建连接完成操作即可,下面选择   “是,在连接字符串中包含敏感数据”

  

 

 

 选择需要添加的数据库对象,点击完成。

2)、操作数据表的增删改查

  2.1)、声明一个 EF的上下文.(这个上下文指向数据库)

  

 

 

 这个对象可以声明成全局的上下文

StudentEntities dbContext = new StudentEntities();

 

  2.2)、增

   1 //声明一个实体,并赋值 

 2 Students stu = new Students();
 3             stu.StudentName = "李四";
 4             stu.StudentSex = "";
 5             stu.StudentAge = 19;
 6             stu.StudentProvince = "上海";
 7             stu.StudentPhone = "17468523001";
 8 
 9             //写法一:
10             //dbContext.Students.Add(stu);
11             //写法二:
12             dbContext.Entry<Students>(stu).State = System.Data.Entity.EntityState.Added;
13 
14         //告诉上下文把实体的变化保存到数据库里面去,返回受影响行数
15             int i = dbContext.SaveChanges();
        //三元表达式
16 string str = i == 1 ? "添加成功" : "添加失败"; 17 Console.WriteLine(str);

  2.3)、删

1  Students stu = new Students();
2             stu.StudentNO = 1833200159;
3 
4             dbContext.Entry<Students>(stu).State = System.Data.Entity.EntityState.Deleted;
5             int i = dbContext.SaveChanges();
6             string str = i == 1 ? "删除成功" : "删除失败";
7             Console.WriteLine(str);

  2.4)、修改整条数据

 1 Students stu = new Students();
 2             stu.StudentNO = 1833200160;
 3             stu.StudentName = "赵六";
 4             stu.StudentSex = "";
 5             stu.StudentAge = 20;
 6             stu.StudentProvince = "广州";
 7             stu.StudentPhone = "18654257894";
 8 
 9             dbContext.Entry<Students>(stu).State = System.Data.Entity.EntityState.Modified;
10             int i = dbContext.SaveChanges();
11             string str = i == 1 ? "修改成功" : "修改失败";
12             Console.WriteLine(str);

  2.4.2)、修改单个属性

 1 Students stu = new Students();
 2             stu.StudentNO = 1833200160;
 3             stu.StudentName = "露丝";
 4             stu.StudentAge = 0;
 5             stu.StudentSex = "";
 6             stu.StudentProvince = "";
 7             stu.StudentPhone = "";
 8 
 9             //将当前实例附加到上下文来处理
10             dbContext.Students.Attach(stu);
11 
12             //写法一:
13             //这里修改了名字和性别,因为其他的属性字段是不可空的所以为了通过验证必须
14             //赋值(赋上了任意的值,但是我们并没有保存这些更改)
15             //dbContext.Entry<Students>(stu).Property("StudentName").IsModified = true;
16             //dbContext.Entry<Students>(stu).Property("StudentSex").IsModified = true;
17 
18             //写法二:Lambda表达式
19             dbContext.Entry<Students>(stu).Property<string>(s => s.StudentName).IsModified = true;
20             dbContext.Entry<Students>(stu).Property<string>(s => s.StudentSex).IsModified = true;
21 
22             int i = dbContext.SaveChanges();
23             string str = i == 1 ? "修改成功" : "修改失败";
24             Console.WriteLine(str);
View Code

  2.5)、查询整张数据表

1 foreach (var item in dbContext.Students)
2             {
3                 //打印整张表的学号和姓名
4                 Console.WriteLine("学号: " + item.StudentNO + "    姓名: " + item.StudentName);
5             }

  2.5.2)、Linp表达式按条件查询数据

1  //查询整张表女生的信息
2             var temp = from s in dbContext.Students
3                        where s.StudentSex == ""
4                        select s;
5             foreach (var item in temp)
6             {
7                 //打印女生的信息
8                 Console.WriteLine(item.StudentName);
9             }

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇.netcore+vue+elementUI 前后端分.. 下一篇.net Ajax与后台一般处理程序(ash..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目