好的,我是MVC的新手,我只是尝试了一个简单的例子,我从数据库中获取数据并在简单的网页中显示它。
我正在做这个教程。
我创建了3个名为Student,Course,Enrollment的文章。
他们是:
public class Student
{
public int StudentId {get; set;}
public string LastName { get; set; }
public string FirstName { get; set; }
// public DateTime EnrollmentDate { get; set; }
public List<Enrollment> Enrollments { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public string CourseName {get; set;}
public int TotalCredits { get; set; }
public List<Enrollment> Enrollments { get; set; }
}
public class Enrollment
{
public int EnrollmentID { get; set; }
public int CourseId { get; set; }
public int StudentId { get; set; }
public decimal? Grade { get; set; }
public Student Student { get; set; }
public Course Course { get; set; }
}
请注意,学生有一个入学列表,与课程相同。那个Enrollments有一个Sudent实例和一个Course实例。这告诉数据库如何将不同的表相互连接。
我在XyzController中创建了一个简单的Action方法,它从数据库中提取数据,将其传递给View并将View()的返回转发给浏览器:
XyzController.cs
public class XyzController : Controller
{
private SchoolContext db = new SchoolContext();
public ActionResult Abc()
{
var students = db.Students.ToList();
var courses = db.Courses.ToList();
var enrollments = db.Enrollments.ToList();
Courses_Students_Enrollments obj = new Courses_Students_Enrollments();
obj.courses = courses;
obj.students = students;
obj.enrollments = enrollments;
return View(obj);
}
这是View(Abc.cshtml文件),它使用Controller中Action方法传递的数据创建View:
Abc.cshtml
@model WebApplication.ViewModels.Courses_Students_Enrollments
@{
ViewBag.Title = "Abc Page";
}
<h2>This is Abc.cshtml under the folder View/Xyz</h2>
@* <h2>Course Tittle: @Model.course.CourseName</h2> *@
<br />
Students:
@foreach (var item in Model.students) {
<h2>Student Name: @item.FirstName @item.LastName</h2>
}
<br />
Courses:
@foreach (var item in Model.courses) {
<h2>Course Tittle: @item.CourseName @item.TotalCredits</h2>
}
<br />
Enrollments:
@foreach (var item in Model.enrollments) {
<h2>Enrollments: @item.CourseId @item.StudentId</h2>
}
现在进入数据库。
这是Class SchoolContext,它扩展了DbContext,并且像数据库接口一样运行(种类)。我在类中放入了我想要的每个类的DbSet。
SchoolContext.cs
public class SchoolContext : DbContext
{ //This class enables CRUD (Create, Read, Update, Delete) functionality
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
}
在从数据库中获取数据之前,有必要引入一些数据。
此类将使用每个表的一些记录初始化数据库。
SchoolInitializer.cs
public class SchoolInitializer : DropCreateDatabaseIfModelChanges<SchoolContext>
{
protected override void Seed(SchoolContext context)
{
var students = new List<Student>
{
new Student{FirstName = "James", LastName = "Dean")},
new Student{FirstName = "Lynda", LastName = "Thames")}
};
foreach (var temp in students)
{
context.Students.Add(temp);
}
var courses = new List<Course>
{
new Course{CourseName = "Java", TotalCredits = 4},
new Course{CourseName = "C#", TotalCredits = 4}
};
foreach (var temp in courses)
{
context.Courses.Add(temp);
}
var enrollments = new List<Enrollment>
{
new Enrollment{StudentId = 1, CourseId = 1, Grade = 3},
new Enrollment{StudentId = 1, CourseId = 2, Grade = 4}
};
foreach (var temp in enrollments)
{
context.Enrollments.Add(temp);
}
--> context.SaveChanges();
}
}
这里在Application_start()
上调用了SchoolInitializer类Global.asax中
namespace WebApplication
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
--> Database.SetInitializer<SchoolContext>(new SchoolInitializer());
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
一切正常,数据库中的数据已由控制器提取,传递给View并显示在浏览器中。但是,在再次运行项目后,修改SchoolInitializer中的记录不会影响数据库,数据库保持不变,并在第一次初始化时使用第一个数据。
{我解决了我写这个问题的问题:)所以,谢谢大家;)}
解决方案:当数据库不存在时,Global.asax中的Application_Start()仅调用SchoolInitializer。一旦创建,ApplicationInstializer将不会被Application_start()调用。
离开这篇文章以防有人可以受益:)