经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
使用ADO.NET实体数据模型
来源:cnblogs  作者:白菜园子呀  时间:2019/10/8 9:33:49  对本文有异议

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

 

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

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

  

 

 

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

  

 

 

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

  

 

 

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

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

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

  

 

 

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

  1. StudentEntities dbContext = new StudentEntities();

 

  2.2)、增

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

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

  2.3)、删

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

  2.4)、修改整条数据

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

  2.4.2)、修改单个属性

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

  2.5)、查询整张数据表

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

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

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

 

原文链接:http://www.cnblogs.com/licm/p/11631561.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号