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

 

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

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

  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             }