简体   繁体   English

如果模型更改,如何在EF中重新创建数据库?

[英]How to recreate database in EF if my model changes?

I created a DbContext like so : 我创建了一个像这样的DbContext:

   public class myDB : DbContext
   {
     public DbSet<Party> Parties { get; set; }
     public DbSet<Booking> Bookings { get; set; }
   }

This generated my DataBase and the two tables above..Great 这生成了我的DataBase和上面的两个表......很棒

I then decided to add another DbSet into the mix & I got an error: 然后我决定在混合中添加另一个DbSet,我收到一个错误:

the model backing the 'Party' context has changed since the database was created 自创建数据库以来,支持“Party”上下文的模型已更改

I'm aware of the fixes for this using modelbuilder.IncludeMetadataInDatabase = false; 我知道使用modelbuilder.IncludeMetadataInDatabase = false;修复此问题modelbuilder.IncludeMetadataInDatabase = false; and Database.SetInitializer<ClubmansGuideDB>(null); Database.SetInitializer<ClubmansGuideDB>(null);

1) What's the correct way to add my new classes to the context and have them generated in the DataBase? 1)将新类添加到上下文并在DataBase中生成它们的正确方法是什么?

2) In my futile attempts to solve this myself I deleted the tables and tried to re-run the app with no success I then deleted the full database and re-run and it doesn't re-generate the DB. 2)我自己试图解决这个问题,我删除了表并试图重新运行应用程序但没有成功我然后删除了完整的数据库并重新运行它并没有重新生成数据库。 How can I start from scratch - is there some cache somewhere? 我怎样才能从头开始 - 某处有缓存吗?

I believe you're looking for: 我相信你在寻找:

Database.SetInitializer<ClubmansGuideDB>(new DropCreateDatabaseIfModelChanges<ClubmansGuideDB>());

As of EF 4.1 and above. 截至EF 4.1及以上版本。

Note that this assumes you have permission to even drop your database. 请注意,这假设您有权删除数据库。 I do this locally for ease of development but disable it in staging/production (I do a manual Schema Compare and push my changes). 我在本地这样做是为了便于开发,但在暂存/生产中禁用它(我做一个手动架构比较并推送我的更改)。

By the way, for testing, if you need to force recreate the database, you can do: 顺便说一下,为了测试,如果你需要强制重新创建数据库,你可以这样做:

using (var context = new ClubmansGuideDB()) {
    context.Database.Initialize(force: true);
}

( using if you don't already have a reference to your DB) using ,如果你还没有到你的数据库的引用)

You can let the Entity Framework recreate the whole database by using a Database Initializer or if you want to migrate data you can look at the Code First Migrations 您可以让Entity Framework使用Database Initializer重新创建整个数据库,或者如果您想迁移数据,可以查看Code First Migrations

The following would recreate your database when the model changes: 以下将在模型更改时重新创建数据库:

Database.SetInitializer<myDB>(new DropCreateDatabaseIfModelChanges<myDB>());

Try putting this in your Global.asax.cs Application_Start() method. 尝试将它放在Global.asax.cs Application_Start()方法中。

Database.SetInitializer<DatabaseContext>(null);

To reset the database from scratch on app run make a class like this 要在app run上从头开始重置数据库,请创建一个这样的类

//Where myDB is your context
public class EntityBase: DropCreateDatabaseAlways<myDB>
{

} 

Then in your Application_Start() method you can do this 然后在Application_Start()方法中,您可以执行此操作

Database.SetInitializer(new EntityBase());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模型更改时重新创建数据库 - Recreate database when model changes EF数据库首先如何更新数据库更改模型? - EF Database first how to update model for database changes? EF 4.4:模型更改未与数据库同步 - EF 4.4: Model changes not syncing with the database 如何首先在EF数据库中将DbEntities对象分配给我的模型对象 - how to assign DbEntities object to my model object in EF Database first EF代码优先迁移:重新创建数据库和种子 - EF Code First Migrations: recreate database and Seed 如何使用EF Core更改Sqlite数据库? - How to make changes to Sqlite database with EF Core? 如何使用EF模型和SDF数据库生成数据库? - How to generate a database with the EF model and SDF database? 无法更新数据库以匹配当前 model,因为有挂起的更改 EF - Unable to update database to match the current model because there are pending changes EF 如果EF Core中的模型更改而没有迁移,则删除数据库 - Drop database if model changes in EF Core without migrations EF Core 3.1:删除了所有迁移和数据库,如何在新数据库中重新创建所有表? - EF Core 3.1: Deleted all Migrations and the Database, how to recreate all tables in a new database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM