首页 新闻 会员 周边

升级到EF4.1 RC 后 seed()方法里面的数据无法写入到 数据库中

0
悬赏园豆:10 [待解决问题] 解决于 2011-04-26 08:20

如题:

我的测试项目升级到EF4.1 RC 后 seed()方法里面的数据无法写入到 数据库中。大师们有没有类似的问题啊:

DBInitializer代码----:

 

1 public class DBInitializer : DropCreateDatabaseIfModelChanges<ConferenceMSEntitiesContext>
2 {
3 protected override void Seed(ConferenceMSEntitiesContext context)
4 {
5 var de = new List<Department> {
6 new Department{
7 Name="小庙",
8 DepartmentTypeID=1
9 },
10 new Department{
11 Name="经济局",
12 DepartmentTypeID=3
13 },
14
15 new Department{
16 Name="发改局",
17 DepartmentTypeID=3,
18
19 }
20
21 };
22
23
24
25 de.ForEach(d => context.Departments.Add(d));
26
27
28
29
30
31
32 }
33
34 }
35 }

 

以上代码最后不论是否添加     base.Seed(context); 和  context.SaveChanges(); 这两行代码,都无法把数据写进数据库。

Application_Start-----------------

 

1 protected void Application_Start()
2 {
3
4 System.Data.Entity.Database.SetInitializer<ConferenceMSEntitiesContext>(new DBInitializer());
5
6 AreaRegistration.RegisterAllAreas();
7
8 RegisterGlobalFilters(GlobalFilters.Filters);
9 RegisterRoutes(RouteTable.Routes);
10 }
问题补充: 自己解决了, 1、有外键关系的表要先填充外键表的数据。 2、填充了之后要加,context.SaveChanges(),代码
高高不高的主页 高高不高 | 初学一级 | 园豆:170
提问于:2011-04-11 08:20
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册