猿问

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

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


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


我的连接字符串:


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

    }

}


倚天杖
浏览 133回答 1
1回答

动漫人物

设置初始化程序后,通过访问某处的上下文或强制它来强制它运行:Database.SetInitializer(new NullDatabaseInitializer<ExamDbContext>());// Forces initialization of database on model changes.using (var context= new ExamDbContext ()) {   context.Database.Initialize(false);}    
随时随地看视频慕课网APP
我要回答