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