【问题标题】:My database is not getting automatically created by Entity Framework 6 code-first approach?我的数据库不是由 Entity Framework 6 代码优先方法自动创建的吗?
【发布时间】:2019-11-10 13:25:48
【问题描述】:

我正在尝试使用代码优先方法为在线考试系统创建数据库。我已经创建了我的数据模型,我的上下文类,在Global.asax 文件中添加了我的setInitializer 方法,添加了我的连接字符串。

但仍然没有创建数据库。真的会需要一些帮助。

我的连接字符串:

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

LAPTOP-JJKI9JN7 是我的 SSMS 服务器名称

学生表:

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

问题表:

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

管理员表:

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

我的上下文类

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>();
        }
    }
}

我的 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文件:

    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);
       }
}

【问题讨论】:

    标签: c# sql-server asp.net-mvc entity-framework-6 connection-string


    【解决方案1】:

    设置初始化程序后,通过访问某处的上下文或强制它来强制它运行:

    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?

    【讨论】:

    • 错误是:非静态字段、方法或属性'Database.Initialize(bool)'需要对象引用
    • 是的,您可能需要在您的上下文中运行它。见编辑。
    • 好的,错误已解决,但它再次没有创建数据库。你能检查我的connectionString并确保没有问题吗?如果是这样,你能告诉我解决这个问题的替代方法吗?
    • 好的,我看到了一个问题。您将初始化程序设置为 null 而不是使用您创建的初始化程序。试试看:Database.SetInitializer(new ExamInitializer &lt;ExamDbContext&gt;());
    • 非常感谢史蒂夫。这实际上解决了我的问题。我需要使用 CreateDatabaseIfNotExists() 而不是 NullDatabaseInitializer()。这创建了数据库。真的是一个很大的帮助。已经卡在上面好几天了。欣赏。
    猜你喜欢
    • 2013-07-10
    • 2016-11-03
    • 2017-05-31
    • 1970-01-01
    • 2013-09-23
    • 2021-05-04
    • 1970-01-01
    • 2019-08-02
    • 2016-03-31
    相关资源
    最近更新 更多