我正在尝试使用代码优先方法为在线考试系统创建一个数据库。我已经创建了我的数据模型,我的上下文类,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; } }}
1 回答
动漫人物
TA贡献1815条经验 获得超10个赞
设置初始化程序后,通过访问某处的上下文或强制它来强制它运行:
Database.SetInitializer(new NullDatabaseInitializer<ExamDbContext>());
// Forces initialization of database on model changes.
using (var context= new ExamDbContext ()) {
context.Database.Initialize(false);
}
- 1 回答
- 0 关注
- 125 浏览
添加回答
举报
0/150
提交
取消