【发布时间】:2014-08-03 13:59:30
【问题描述】:
在回答之前,请阅读整个问题。教程中的所有内容,堆栈 overflow 主题都不起作用。
我尝试强制实体框架在每次运行调试程序时将记录插入数据库,即使根本没有任何更改。这意味着如果我在上次运行期间进行任何更改,它们将消失。这不会发生。
我的 Global.asax:
protected void Application_Start() {
Database.SetInitializer(new MigrateDatabaseToLatestVersion<PersonContext, Configuration>());
Database.SetInitializer(new DropCreateDatabaseAlways<PersonContext>());
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
我的 Web.config 实体框架部分:
<entityFramework>
<contexts>
<context type="WebApplication2.Models.PersonContext, WebApplication2">
<databaseInitializer type="WebApplication2.Models.PersonInitializer, WebApplication2" />
</context>
</contexts>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
我的种子方法:
protected override void Seed(PersonContext context) {
var persons = new List<Person> {
new Person{FirstName = "John", LastName = "Doe", CellNumber = "123-456-789", SecondaryPhoneNumber = "98873213", Address = "1street 2",BirthDate = DateTime.Now.Date, Pesel = "312312312", Notes = "Annoying"},
new Person{FirstName = "Anna", LastName = "Doe", CellNumber = "113-456-789", SecondaryPhoneNumber = "98873213", Address = "1street 2",BirthDate = DateTime.Now.Date, Pesel = "548555672", Notes = "Less Annoying"}
};
persons.ForEach(person => context.Persons.Add(person));
context.SaveChanges();
var meetings = new List<Meeting>{
new Meeting{PersonId = 1, Body = "Body of meeting", Date = DateTime.Now}
};
meetings.ForEach(meeting => context.Meetings.Add(meeting));
context.SaveChanges();
var statuses = new List<Status> {
new Status{Name = "OK"},
new Status {Name = "NOT_OK"}
};
statuses.ForEach(status => context.Statuses.Add(status));
context.SaveChanges();
}
编辑:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication2.Models {
public class PersonInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<PersonContext> {
protected override void Seed(PersonContext context) {
var persons = new List<Person> {
new Person{FirstName = "John", LastName = "Doe", CellNumber = "123-456-789", SecondaryPhoneNumber = "98873213", Address = "1street 2",BirthDate = DateTime.Now.Date, Pesel = "312312312", Notes = "Annoying"},
new Person{FirstName = "Anna", LastName = "Doe", CellNumber = "113-456-789", SecondaryPhoneNumber = "98873213", Address = "1street 2",BirthDate = DateTime.Now.Date, Pesel = "548555672", Notes = "Less Annoying"}
};
persons.ForEach(person => context.Persons.Add(person));
context.SaveChanges();
var meetings = new List<Meeting>{
new Meeting{PersonId = 1, Body = "Body of meeting", Date = DateTime.Now}
};
meetings.ForEach(meeting => context.Meetings.Add(meeting));
context.SaveChanges();
var statuses = new List<Status> {
new Status{Name = "OK"},
new Status {Name = "NOT_OK"}
};
statuses.ForEach(status => context.Statuses.Add(status));
context.SaveChanges();
}
}
}
【问题讨论】:
-
包含
Seed方法的类名是什么,该类的基类是什么? -
@YuliamChandra 我在 OP 的 EDIT 中发布了完整的课程代码。执行迁移后调用 Seed 方法,因此我在数据库中有这些数据,但我希望它始终被调用。
标签: asp.net-mvc entity-framework seeding