简体   繁体   English

实体框架会删除未创建的表吗?

[英]Will Entity Framework delete tables that it didn't create?

I have an existing database. 我有一个现有的数据库。 It has tables that were created manually (in SQL, not with EF). 它具有手动创建的表(在SQL中,而不是在EF中)。 I have to add EF Code First Migrations, for reasons. 由于某些原因,我必须添加EF代码优先迁移。

When Update-Database is run on an environment that hasn't had its migrations applied yet, I want EF to create the tables, but I DON'T want it to drop the whole database. 当Update-Database在尚未应用其迁移的环境上运行时,我希望EF创建表,但我不希望它删除整个数据库。 Is that what it will do, and if so how can I stop it? 这就是它的作用,如果是的话,我该如何制止它?

In my database context I'm not sure whether to use 在我的数据库上下文中,我不确定是否要使用

//Disable initializer
Database.SetInitializer<ApplicationDbContext>(null);

or to use 或使用

Database.SetInitializer<ApplicationDbContext>(new CreateDatabaseIfNotExists<ApplicationDbContext>());

According to the documentation it looks like CreateDatabaseIfNotExists will actually still drop the database if the EF tables don't exist. 根据文档,如果EF表不存在,则看起来CreateDatabaseIfNotExists实际上仍然会删除数据库。

But if I just disable the initializer then I think that means it won't even create the EF tables. 但是,如果我只是禁用初始化程序,那么我认为这意味着它甚至都不会创建EF表。 Neither of these solutions seem good. 这些解决方案似乎都不好。

If you have some tables in your db, that created manually, you can enable migration with following commands: 如果数据库中有一些手动创建的表,则可以使用以下命令启用迁移:

PM>Enable-Migrations
PM>AddMigration [A_Name_For_Your_Migration] -IgnoreChanges
PM>Update-Databases

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM