三层架构下的EntityFramework codefirst
好久没写博客了,今天研究了EF框架的CodeFirst模式,从字面意思可以看出,代码优先.所谓代码优先,与以往的添加ado.net不同,主要是编写代码生成数据库和数据表,生成数据实体映射。个人感觉这种方法相比较自动添加数据集的方式是不错的,但是有一个缺点就是,你编写的生成数据库和数据表的代码一旦写好,改起来就比较麻烦,就算改动了一个属性,就得将数据库删掉,重新运行代码(不删也行,但是需要更新数据库,比较麻烦)。好了,请看代码:
1.项目使用三层架构,在数据访问层用的是EF框架
2.在Model层新建Score和StudentModel类,内容如下

1 namespace Students.Model 2 { 3 public class Score 4 { 5 /// <summary> 6 /// Gets or sets 分数ID 7 /// </summary> 8 public int ScoreID { get; set; } 9 10 /// <summary> 11 /// Gets or sets 学生信息 12 /// </summary> 13 public virtual StudentsModel Student { get; set; } 14 15 public virtual int? StudentId { get; set; } 16 17 /// <summary> 18 /// Gets or sets 学生分数 19 /// </summary> 20 public decimal StudentScore { get; set; } 21 } 22 }

1 namespace Students.Model 2 { 3 public class StudentsModel 4 { 5 [Key] 6 public int StudentID { get; set; } 7 8 /// <summary> 9 /// Gets or sets 学生学号 10 /// </summary> 11 public string StudentNumber { get; set; } 12 13 /// <summary> 14 /// Gets or sets 学生姓名 15 /// </summary> 16 public string StudentName { get; set; } 17 } 18 }
3.在DAL层新建一个StudentContext类,主要是构造实体对象(该类继承DbContext)

1 namespace Students.DAL 2 { 3 public class StudentContext:DbContext 4 { 5 //构造实例模型 6 public DbSet<StudentsModel> Student { get; set; } 7 8 public DbSet<Score> Score { get; set; } 9 } 10 }
4.配置Web.config,将连接字符串写入配置文件里

1 <add name="StudentContext" connectionString="Data Source=PC201307311548;Initial Catalog=Student;Integrated Security=True" providerName="System.Data.SqlClient"/>
5.实现方法

1 //实例化数据源连接信息 2 private StudentContext context; 3 4 public ScoreDAL() 5 { 6 context = new StudentContext(); 7 } 8 9 ///添加学生分数 10 public bool Add(Score score,string stuNum) 11 { 12 //判断学生是否存在 13 var student = this.context.Student.Where(p => p.StudentNumber == stuNum).FirstOrDefault(); 14 try 15 { 16 if (student == null) 17 { 18 return false; 19 } 20 else 21 { 22 score.Student = student; 23 context.Score.Add(score); 24 context.SaveChanges(); 25 context.Dispose(); 26 return true; 27 } 28 } 29 catch (Exception e) 30 { 31 throw e; 32 } 33 } 34 35 //修改学生分数 36 public bool Update(string stuNum,decimal score) 37 { 38 //查询学生是否有分数 39 var stuscore = this.context.Score.Where(p => p.Student.StudentNumber == stuNum).FirstOrDefault(); 40 41 if (stuscore == null) 42 { 43 return false; 44 } 45 else 46 { 47 stuscore.StudentScore = score; 48 this.context.SaveChanges(); 49 } 50 51 return true; 52 } 53 54 //删除学生信息 55 public bool Delete(string scoreId) 56 { 57 try 58 { 59 int id=int.Parse(scoreId); 60 //查询学生成绩是否存在 61 var score = this.context.Score.Where(p => p.ScoreID == id).FirstOrDefault(); 62 63 //如果存在则执行删除,不存在则返回信息 64 if (score == null) 65 { 66 return true; 67 } 68 else 69 { 70 //删除成绩信息,提交到数据库执行 71 this.context.Score.Remove(score); 72 this.context.SaveChanges(); 73 } 74 } 75 catch 76 { 77 return false; 78 } 79 80 return true; 81 }
6.实现效果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?