Drop or Recreate database with Entity Framework Mi

2019-03-26 05:55发布

Which is the command to recreate or drop the database when using Entity Framework Migrations, not Initializers?

What should I write on the package manager console?

COMMENT:

I'm looking for some command that gives me the same functionality as Database.SetInitializer<>(new DropCreateDatabaseIfModelChanges<>()); but with the migrations approach.

3条回答
男人必须洒脱
2楼-- · 2019-03-26 06:29

I went into server explorer and manually deleted all the tables. Then went to the project\Migrations folder and deleted all the migration scripts.

Then a plain old Update-Database -Force did what I needed.

查看更多
贼婆χ
3楼-- · 2019-03-26 06:34

You can get the same behavior with Migrations by using automatic migrations.

PM> enable-migrations -EnableAutomaticMigrations

In Configuration.cs in the constructor, make sure automatic migrations and allow data loss are set to true...

public Configuration()
{
    AutomaticMigrationsEnabled = true;
    AutomaticMigrationDataLossAllowed = true;
}

Now whenever your model changes, just execute update-database...

PM> update-database

The schema will be changed to match the models and any existing data in the tables will stay there (unless it's in a renamed or removed column). The Seed method is run after the database is updated so you can further control any changes to the data in there.

查看更多
Viruses.
4楼-- · 2019-03-26 06:49

In VS2015, I did the following to resolve it: In solution explorer menu-->Show All-->The App_Data folder shows the LocalDb mdf file-->Right click the file and click Delete-->In Package Manage Condsole, run: Update-Database -Force

查看更多
登录 后发表回答