0

我最近重组了我的 MVC 4 应用程序,现在面临的问题是数据库不再被初始化。

在 Global.asax 中,我在第一次启动时以这种方式调用初始化程序:

Database.SetInitializer<MyContext>(new MyInitializer());
new MyContext().Portfolios.Find(1);
new MyContext().UserProfiles.Find(1);

MyInitializer 包含一个小种子方法。

public class MyInitializer : CreateDatabaseIfNotExists<MyContext>
{
    protected override void Seed(MyContext context)
    {
 ...

Initializer 和 Context 在我在 Global.asax 中引用的命名空间 .DAL 中。数据库是空的,我希望它被创建,但它没有。

似乎根本没有调用 Initializer..如果我将 Initializer 设置为 DropCreateDatabaseAlways 并且我不明白为什么,这没有什么区别。你们有没有一个想法或一个好的链接?

4

1 回答 1

0

I apologize for my question ... it did not contain the info that was necessary to answer it. When I said that I restructured my project, I should have written instead that I added lots of inheritance where formerly, there was no inheritance. Seems like in OnModelCreating, an entry modelBuilder.Entity(); is needed in order for MVC to consider that entity as included.

于 2013-05-29T13:37:02.373 回答