创建数据上下文

数据库上下文

using ContosoUniversity.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace ContosoUniversity.DAL
{
    public class SchoolContext : DbContext
    {

        public SchoolContext()
            : base("SchoolContext")
        {
        }

        public DbSet<Student> Students { get; set; }
        public DbSet<Enrollment> Enrollments { get; set; }
        public DbSet<Course> Courses { get; set; }

        /// <summary>
        /// 在 OnModelCreating 方法中的代码防止数据库中的表名被多元化。
        /// 如果没有这样处理的话,生成的表将会被命名为 Students,Courses 和 Enrollments。
        /// 现在,表名将会被命名为 Student,Course 和 Enrollment。
        /// 开发者可以决定表名是否被多元化。
        /// 这个教程使用单数模式,但是重要的是你可以这行代码来选择喜欢的模式
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}
原文地址:https://www.cnblogs.com/yechangzhong-826217795/p/4669637.html