数据未显示在特定表中,只有一个表包含数据

时间:2015-05-08 22:21:51

标签: c# asp.net asp.net-mvc database visual-studio

出于某种原因,只有学生的数据才会在学生表中生成。但是,注册和课程表中没有数据,即使我在SchoolInitializer中添加它们。我错过了什么吗?有什么建议?启动我的应用程序时,我会关闭与数据库的连接,重建解决方案,然后在不调试的情况下启动,单击创建数据的链接,然后检查我的表。

namespace MyFirstWebApplication.Models
{
    public class SchoolInitializer : DropCreateDatabaseIfModelChanges<SchoolContext>
    {
        protected override void Seed(SchoolContext context)
        {
            var students = new List<Student>
            {
                new Student{FirstName = "James", LastName= "Dean", EnrollmentDate = DateTime.Parse("2014-01-02") },
                new Student{FirstName = "Lynda", LastName = "Thames", EnrollmentDate = DateTime.Parse("2013-11-02")}
            };

            foreach (var temp in students)
            {
                context.Students.Add(temp);
            }
            context.SaveChanges();

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

            context.SaveChanges();

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

namespace MyFirstWebApplication.Models
{
    public class SchoolContext : DbContext
    {   //enables CRUD functionality
        public DbSet<Student> Students { get; set; }
        public DbSet<Course> Courses { get; set; }
        public DbSet<Enrollment> Enrollments { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}

namespace MyFirstWebApplication
{
    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);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

最后只尝试拨打context.SaveChanges()一次。

另请查看以下SO问题/答案:

Multiple SaveChanges Calls in Entity Framework

Call SaveChanges twice inside transaction

答案 1 :(得分:0)

如果您使用Identity字段作为密钥,则应获取生成的数据库密钥。例如,您应该使用:

var newStudent = context.Students.Add(new Student{...});
context.SaveChanges();
var newStudentId = newStudent.StudentId

然后您可以使用此ID来插入您的注册。这样的事情:

context.Enrollments.Add(new Enrollment{StudentId = newStudentId, CourseId = 1, Grade = 3});

您必须考虑的另一件事是Seed方法调用savechanges方法。

您应该查看 AddOrReplace 方法。它是为种子数据库插入数据的首选方法。