简体   繁体   English

对现有数据库使用实体框架。 防止表删除?

[英]Using Entity Framework with an existing database. Prevent Table Deletion?

I'm updating an old website's forums to the open source MVCForum . 我正在将旧网站的论坛更新为开源MVCForum MVCForum uses Code First Entity Framework which from what I can tell makes it a tricky to integrate with an existing database(at least for a beginner like me :/). MVCForum使用Code First Entity Framework,据我所知,它很难与现有数据库集成(至少对于像我这样的初学者而言:: /)。 I've worked with entity framework briefly from scratch, but never in an already designed application with models completed. 我从头开始就使用实体框架进行过短暂的工作,但是从未在模型已经完成的已经设计好的应用程序中工作过。

Is there a way to integrate a Code First EF project's required tables into a database without removing all tables that aren't directly associated with said project? 有没有一种方法可以将Code First EF项目的所需表集成到数据库中,而又不删除与该项目没有直接关联的所有表? Having all the website's tables in one database and not just the forums' tables would be ideal. 将网站的所有表格都放在一个数据库中,而不仅仅是论坛的表格都将是理想的选择。

Also, is there any way to force an update to a database through Entity Framework? 另外,是否有任何方法可以通过实体框架强制对数据库进行更新? Just a brute force "Check if these tables exist, if not create them." 只是蛮力“检查这些表是否存在,如果不创建它们。” I've run into the issue of the EF thinking my database has the necessary tables(I think that's the problem...) and it doesn't. 我遇到了EF的问题,认为我的数据库有必要的表(我认为是问题所在……),而事实并非如此。

Apologies if this doesn't make sense. 道歉,如果这没有道理。 I may not even understand EF enough to properly ask questions about it. 我什至对EF都不足够了解,无法正确地提出有关它的问题。

Thanks 谢谢

Yes, if your DbSet names in Model will be unique (vs. DB table names), EF just create __MigrationHistory table and add new tables corresponding to your EF model. 是的,如果您在Model中的DbSet名称是唯一的(与DB表名称相对),则EF只需创建__MigrationHistory表并添加与您的EF模型相对应的新表即可。 This is default behavior with CreateDatabaseIfNotExists initializer. 这是CreateDatabaseIfNotExists初始化程序的默认行为。

In your case you should not use : 在您的情况下, 您不应使用

  • DropCreateDatabaseWhenModelChanges
  • DropCreateDatabaseAlways

because this initialisers will drop the database. 因为此初始化程序将删除数据库。

Reverse engineering 逆向工程

Optionally you can just create your new tables in DB and then use EF code first common way. (可选)您可以只在DB中创建新表,然后首先使用EF代码。 In this case EF will not change your DB. 在这种情况下,EF不会更改您的数据库。 Notice that in this case you must make all changes in your DB manually. 注意,在这种情况下,您必须在数据库中手动进行所有更改。

That project looks prepped for migrations so all you should need to do is point your connection string where you want. 该项目为迁移做好了准备,因此您所需要做的就是将连接字符串指向所需的位置。 On first run it will create the database so now you want to create a baseline migration that takes a snapshot of the existing schema: 首次运行时,它将创建数据库,因此现在您要创建一个基线迁移,该基线迁移现有架构的快照:

1) add-migration Initial -IgnoreChanges  // tells EF tables exist
2) update-database  // Adds record to __MigrationHistory table

Now you can make your own changes and follow the normal code first workflow: 现在,您可以进行自己的更改,并遵循常规代码优先工作流程:

1) Change schema
2) add-migration foo
3) update-database

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

相关问题 使用Entity Framework将模型映射到现有数据库表 - Map model to existing database table using Entity Framework 实体框架6,现有数据库。 相关数据,但没有导航属性。 我迷路了 - Entity Framework 6, existing Database. Related data but no Navigation property. I'm lost 使用实体框架通过MSSQL数据库进行级联删除 - Cascade Deletion through MSSQL Database using Entity Framework 使用实体框架在创建新实体时向数据库中的现有表添加新记录 - Add a new record to existing table in the database on a new entity creation using Entity Framework 使用 Entity Framework 6 将父实体与数据库中现有的子实体连接起来 - Connect parent entity with existing child entity in database using Entity Framework 6 数据库中已删除的表。 如何获得实体框架以进行重塑? - Deleted table in database. How do I get entity framework to remake it? 实体框架(现有数据库) - Entity Framework (existing database) 使用Entity Framework映射到现有表 - Mapping to existing table using Entity Framework 在数据库优先实体框架中更新 model 时如何防止 DbContext 中的代码删除 - How to prevent come code deletion in DbContext while updating model in database first Entity Framework 使用带有现有子实体的实体框架在数据库中创建实体时出现问题 - Problem creating an entity in the database using Entity Framework with existing child entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM