[英]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.