7

我正在使用代码优先实体框架,我想知道在创建数据库时如何运行一些代码,以便我可以用一些数据填充我的数据库。(请注意,我要求创建数据库,而不是每次应用程序启动时)。

有人知道我是否可以为此使用方法或事件吗?

4

2 回答 2

11

I'd like to add to Ladislav's answer. The article he pointed to shows how to create an initializer but does not show how to use an initializer to populate a newly created database. DbInitializer has a method called Seed that you can overwrite. You can see an example of how to use this in this article (or video if you prefer, which is on the same page) http://msdn.microsoft.com/en-us/data/hh272552

于 2011-11-24T01:25:04.887 回答
7

您可以使用自定义数据库初始化程序- 这意味着IDatabaseInitializer通过调用来实现和注册此初始化程序Database.SetInitializer

于 2011-01-04T09:10:25.653 回答