zoukankan      html  css  js  c++  java
  • 三层架构下的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 }
    Score
     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 }
    StudentsModel

    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 }
    StudentContext

     4.配置Web.config,将连接字符串写入配置文件里

    1     <add name="StudentContext" connectionString="Data Source=PC201307311548;Initial Catalog=Student;Integrated Security=True" providerName="System.Data.SqlClient"/>
    配置Web.config

     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         }
    ScoreDAL示例

    6.实现效果

  • 相关阅读:
    centos7 源码安装python3
    gitlab 迁移
    gitlab 搭建
    亚伦史沃茨 公开信
    误删了sudo包怎么办
    在中国历史上,我个人最喜欢的三个皇帝
    安装PHP7
    Mac OS上设置Django开发环境
    The Pragmatic Programmer Quick Reference Guide
    Nginx的第一个模块-HelloWorld
  • 原文地址:https://www.cnblogs.com/AlphaThink-AT003/p/3231407.html
Copyright © 2011-2022 走看看