c#实体框架不初始化数据库

时间:2022-05-02 14:10:16

I'm trying to build a WPF database-based application with EF code-first. I had a problem of a not generated database, that I tried to solve like this:

我正在尝试用EF代码构建一个基于WPF的基于数据库的应用程序。我遇到了一个没有生成数据库的问题,我试图这样解决:

using (var db = new MyDBContext())
{
    db.Database.Initialize(true);
}

I've inserted this code in App.xamls.cs, overriding OnStartup method (this works).

我在App.xamls中插入了这段代码。cs,重写OnStartup方法(这是有效的)。

Then I tried to seed some default data like this:

然后我试着输入一些像这样的默认数据:

public class MyDBContextSeeder : DropCreateDatabaseAlways<MyDBContext>
{
    protected override void Seed(MyDBContext context)
    {
        IList<Dog> defaultDog = new List<Dog>();
        defaultDog.Add(new Dog("Spike"));

        foreach (Dog d in defaultDog)
            context.Dogs.Add(d);

        // more data here....

        base.Seed(context);
    }
}

And, since I'm using a custom initializer, I've changed my the first code above like this:

而且,由于我使用的是自定义初始化器,所以我对上面的第一个代码做了如下修改:

using (var db = new PizzeriaDBContext())
{
    new MyDBContextSeeder().InitializeDatabase(db);    //this line
    db.Database.Initialize(true);
}

The problem now is:

现在的问题是:

  • when I build my application my default data is not inserted. I'm really sure of that.

    在构建应用程序时,不会插入默认数据。我很确定。

  • if I try to build my application again, I have an exception like:

    如果我试图再次构建我的应用程序,我有一个例外,比如:

    Dog entity is already in database.

    Dog实体已经在数据库中。

Migrations are enabled.

迁移被启用。

I already tried to delete db.Database.Initialize(true), but when I do this, my database it's not even created.

我已经尝试删除db.Database.Initialize(true),但当我这样做时,我的数据库甚至都没有创建。

1 个解决方案

#1


0  

By default your database is using the CreateDatabaseIfNotExits Initializer. That is why you are getting your exception. So i think what you need to do is the following:

默认情况下,您的数据库使用的是CreateDatabaseIfNotExits初始化器。这就是为什么你会有例外。所以我认为你需要做的是:

On your overriden OnStartup you can use

在您的overriden On startup中,您可以使用

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDBContext,Configuration>)

For this to work you need to change your Configuration.cs class from internal sealed to Public

要使其工作,您需要更改配置。cs类从内部密封到公开

For reference you can look at the very bottom of this article MigrateDatabaseToLatestVersion

作为参考,您可以查看本文的最底层MigrateDatabaseToLatestVersion

#1


0  

By default your database is using the CreateDatabaseIfNotExits Initializer. That is why you are getting your exception. So i think what you need to do is the following:

默认情况下,您的数据库使用的是CreateDatabaseIfNotExits初始化器。这就是为什么你会有例外。所以我认为你需要做的是:

On your overriden OnStartup you can use

在您的overriden On startup中,您可以使用

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDBContext,Configuration>)

For this to work you need to change your Configuration.cs class from internal sealed to Public

要使其工作,您需要更改配置。cs类从内部密封到公开

For reference you can look at the very bottom of this article MigrateDatabaseToLatestVersion

作为参考,您可以查看本文的最底层MigrateDatabaseToLatestVersion