简体   繁体   English

我的数据库不是通过Entity Framework 6代码优先方法自动创建的吗?

[英]My database is not getting automatically created by Entity Framework 6 code-first approach?

I am trying to create a database using code first approach for an online exam system. 我正在尝试使用代码优先方法为在线考试系统创建数据库。 I have already created my data models, my context class, added my setInitializer method in the Global.asax file, added my connection string. 我已经创建了数据模型,上下文类,在Global.asax文件中添加了setInitializer方法,添加了连接字符串。

But still the database isn't getting created. 但是仍然没有创建数据库。 Would really use some help. 真的会使用一些帮助。

My connection string : 我的连接字符串:

<connectionStrings>
    <add name="ExamDbContext" 
         connectionString="server=LAPTOP-JJKI9JN7; Initial Catalog=OnlineExamSystem; Integrated Security=true;" 
         providerName="System.Data.SqlClient">
    </add> 
</connectionStrings>

LAPTOP-JJKI9JN7 is my SSMS server name LAPTOP-JJKI9JN7是我的SSMS服务器名称

Student table: 学生桌:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Online_Exam_System.Models
{
    public class TBL_STUDENT
    {
        public int S_ID { get; set; }

        [Display(Name = "Student")]
        [Required(ErrorMessage = "The field is required")]
        public string S_NAME { get; set; }

        [Display(Name = "Password")]
        [Required(ErrorMessage = "The field is required")]
        public string S_PASSWORD { get; set; }

        [Display(Name = "Marks")]
        [Required(ErrorMessage = "The field is required")]
        public Nullable<int> S_MARKS { get; set; }
    }
}

Question table : 问题表:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Online_Exam_System.Models
{
    public class TBL_QUESTIONS
    {
        public int QUESTION_ID { get; set; }

        [Display(Name = "Question")]
        [Required(ErrorMessage = "The field is required")]
        public string QUESTION_TEXT { get; set; }


        public string OPTION { get; set; }

        [Display(Name = "OPTION A")]
        [Required(ErrorMessage = "The field is required")]
        public string OPTIONA { get; set; }

        [Display(Name = "OPTION B")]
        [Required(ErrorMessage = "The field is required")]
        public string OPTIONB { get; set; }

        [Display(Name = "OPTION C")]
        [Required(ErrorMessage = "The field is required")]
        public string OPTIONC { get; set; }

        [Display(Name = "OPTION D")]
        [Required(ErrorMessage = "The field is required")]
        public string OPTIOND { get; set; }

        public string CORRECT { get; set; }
    }
}

Admin table: 管理员表:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Online_Exam_System.Models
{
    public class TBL_ADMIN
    {
        public int AD_ID { get; set; }

        [Display(Name = "User Name")]
        [Required(ErrorMessage = "The field is required")]
        public string AD_NAME { get; set; }

        [Display(Name = "Password")]
        [Required(ErrorMessage = "The field is required")]
        [DataType(DataType.Password)]
        public string AD_PASSWORD { get; set; }
    }
}

My context class 我的上下文课

using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using Online_Exam_System.Models;
using System.Linq;
using System.Web;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace Online_Exam_System.Data_Access_Layer
{
    public class ExamDbContext : DbContext
    {
        public ExamDbContext() : base("ExamDbContext")
        {
        }

        public DbSet<TBL_ADMIN> TBL_ADMIN { get; set; }
        public DbSet<TBL_QUESTIONS> TBL_QUESTIONS { get; set; }
        public DbSet<TBL_EXAM> TBL_SETEXAM { get; set; }
        public DbSet<TBL_STUDENT> TBL_STUDENT { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}

My Initializer method (it's inside the Model folder) 我的Initializer方法(位于Model文件夹中)

using Online_Exam_System.Data_Access_Layer;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace Online_Exam_System.Models
{
    public class ExamInitializer : CreateDatabaseIfNotExists<ExamDbContext>
    {
        protected override void Seed(ExamDbContext context)
        {
            base.Seed(context);
        }
    }
}

Global.asax file: Global.asax文件:

    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Optimization;
    using System.Web.Routing;

    namespace Online_Exam_System
    {
            public class MvcApplication : System.Web.HttpApplication
            {
                protected void Application_Start()
                {
                    Database.SetInitializer(new 
                    NullDatabaseInitializer<ExamDbContext>());
                    AreaRegistration.RegisterAllAreas();

                    FilterConfig.RegisterGlobalFilters
                    (GlobalFilters.Filters);
                    RouteConfig.RegisterRoutes(RouteTable.Routes);
                    BundleConfig.RegisterBundles(BundleTable.Bundles);
       }
}

After you set your initializer, force it to run by either accessing the context somewhere or forcing it: 设置初始化程序后,通过访问某个地方的上下文或强制它来强制其运行:

Database.SetInitializer(new NullDatabaseInitializer<ExamDbContext>());
// Forces initialization of database on model changes.
using (var context= new ExamDbContext ()) {
   context.Database.Initialize(false);
}    

Forcing code-first to always initialize a non-existent database? 强制代码优先始终初始化不存在的数据库?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据实体代码优先方法播种我的数据库 - Seeding my database as per the Entity code-first approach 遵循Entity Framework代码优先方法,为什么不创建连接字符串? (未创建数据库) - Following Entity Framework code-first approach, why is connection string not created? (No DB created) 实体框架代码优先数据库未更新 - Entity Framework Code-First Database Not Updating 实体框架 5 代码优先不创建数据库 - Entity Framework 5 code-first not creating database 实体框架优先代码和现有数据库 - Entity Framework code-first and existing database 是否可以在不使用Entity Framework中以代码优先方式使用C#属性的情况下生成数据库字段? - Is it possible to generate database fields without using C# properties in Entity Framework, code-first approach? 如果我们使用具有代码优先方法的Entity框架,是否可以在给定路径上创建数据库(Sql Server compact)? - Is possible to create a database (Sql Server compact) on a given path if we use Entity framework with code-first approach? 如何使用 Entity Framework Code-First 方法将 double[] 数组存储到数据库 - How to store double[] array to database with Entity Framework Code-First approach Struct with Entity Framework 的变通方法,代码优先方法 - Work-around for Struct with Entity Framework, Code-First approach 先在Entity Framework代码中自动生成存储过程 - Generate stored procedures automatically in Entity Framework code-first
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM