Я пытаюсь создать базу данных с использованием подхода «сначала код» для системы онлайн-экзаменов.Я уже создал свои модели данных, мой класс контекста, добавил мой метод 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; }
}
}
Вопрос Таблица:
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>();
}
}
}
Метод My 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:
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);
}
}