zoukankan      html  css  js  c++  java
  • 笔试考试系统--配置EF;运行单元测试

    今天完成了ExamOnLine.Service层,EF和ExamDbContext的配置

    工作量相对来说还是比较大的,

    EF的配法:

    首先配置Entities

    每一个Entities对应数据库中的一张表,里面写这些内容

     public class StudentEntities
        {
            public int Id { get; set; }
            /// <summary>
            /// 学生账号
            /// </summary>
            public string StudentNumber { get; set; }
            /// <summary>
            /// 姓名
            /// </summary>
            public string StudentName { get; set; }
            /// <summary>
            /// 密码
            /// </summary>
            public string StudentPwd { get; set; }
            /// <summary>
            /// 学生性别
            /// </summary>
            public string StudentSex { get; set; }
            /// <summary>
            /// 主修专业
            /// </summary>
            public string StudentMajor { get; set; }
            /// <summary>
            /// 学生所在班级
            /// </summary>
            public string StudentClass { get; set; }
            /// <summary>
            /// 学生所在学校
            /// </summary>
            public string StudentShcool { get; set; }
    
        }

    接下来配Config

     里面代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.Entity.ModelConfiguration;
    using ExamOnLine.Service.Entities;
    
    namespace ExamOnLine.Service.Configs
    {
        //和数据库表的对应关系
        //让程序EF自动生成数据库表
        class StudentConfig:EntityTypeConfiguration<StudentEntities>
        {
            public StudentConfig()
            {
                ToTable("Student");
                //姓名这一列是必须的,并且最大长度是20
                Property(e => e.StudentName).IsRequired().HasMaxLength(20);
    
            }
        }
    }

    陪完这些,接下来就是ExamDbContext文件的书写,添加ExamDbContext

    内容代码如下:

     public class ExamDbContext:DbContext
        {
            private static ILog log = LogManager.GetLogger(typeof(ExamDbContext));//声明Log4NET对象,建议一个类就声明一个ILog对象
            public ExamDbContext():base("name=connStr")
            {
                //将EF生成的SQL语句记录在日志里面
                this.Database.Log = (sql) =>
                {
                    log.DebugFormat("EF开始执行sql语句{0}", sql);
                };
    
                Database.SetInitializer<ExamDbContext>(null);//只要数据库建造好后,就加上这句话,禁止Ef再去帮你创建数据库的一些操作
            }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
                modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());
            }
    
            //属性类
            public DbSet<StudentEntities> Students { get; set; }
    
            public DbSet<QuestionsEntities> Questionses { get; set; }
            public DbSet<StudentAnswerEntities> StudentAnswers { get; set; }
            public DbSet<StudentExamInfoEntities> StudentExamInfoes { get; set; }
            public DbSet<StudentScoreEntities> StudentScores { get; set; }
            public DbSet<SubjectEntities> Subjects { get; set; }
            public DbSet<TeacherEntities> Teachers { get; set; }
            public DbSet<TestPaperEntities> TestPapers { get; set; }
    
            public DbSet<TestPaperSubjectEntities> TestPaperSubjects { get; set; }
    
        }

    配完这些,EF的内容代码部分就基本上写完了

    但是后面的操作一样很重要,

    在页面部分 Web.config文件中添加连接字符串,前后端都要加

      <!--连接字符串-->
      <connectionStrings>
        <add name="connStr" connectionString="Data Source=.;Initial Catalog=HPStudentExamSystem;Integrated Security=True" providerName="System.Data.SqlClient"/>  
      </connectionStrings>

    接着添加单元测试

     

     ,单元测试同意需要安装Log4.NET和 EntityFrameWork

    之后运行测试即可

    如果你的测试是这样的,那就通过了

    如果是这样的

     就根据错误信息,查找问题,处理后再测试即可

     ,我今天就是遇到了这样的问题,原因是数据库的主外键关系没有在表中体现,后来改了之后就好了

    程序遇到问题是非常影响项目进度的,希望以后自己代码逻辑都可以再熟练一些吧,加油!

  • 相关阅读:
    平分糖果——BZOJ 1045
    浙大月赛——ZOJ Problem Set 3574
    jsp 自定义标签的写法
    C#扩展方法(转贴)
    window mobile 防止系统休眠代码
    jbpm sql使用动态参数方法
    spring 多数据源配置实现
    原创jquery蒙版控件
    jbpm 错误解决方法
    cas server 配置
  • 原文地址:https://www.cnblogs.com/JuneDream/p/13332253.html
Copyright © 2011-2022 走看看