为什么Entity Framework的EF迁移添加迁移步骤需要数据库连接字符串? [英] Why does Entity Framework's EF Migrations Add-Migration step require a database connection string?
问题描述
我正在尝试使用和理解EF迁移(使用EF 4.3.1,代码优先)。为了支持一个新的改变,我必须使用这样的命令:
I am trying to use and understand EF Migrations (using EF 4.3.1, Code First). In order to scaffold a new change, I have to use a command like this:
Add-Migration MyMigration
-ConnectionString "Data Source=.;Initial Catalog=mydb;"
-ConnectionProviderName "System.Data.SqlClient"
-StartUpProjectName MyWebsite
-ProjectName MyEF.Migrations
为什么添加迁移需要连接字符串数据? 更新数据库
需要一个,这是有道理的。但是,添加迁移是否具有DbContext和配置所需的一切?
Why does Add-Migration require connection string data? Update-Database
needs one, that makes sense. But doesn't Add-Migration have everything it needs from the DbContext and the Configuration?
这不是简单的空闲奇怪,给它一个数据库是非常困惑的,因为我们有一个多租户的东西,其中所需的数据库是灵活的,可以从请求转变为请求,更不用说在静态编译时。因此,如果 Add-Migration
实际上是使用该数据库的任何东西,我们有一个问题。
It's not simply idle wonder, it's very confusing to give it a database, because we have a "multi-tenancy" thing where the desired database is flexible and may change from request to request, let alone at static compile time. So if Add-Migration
is actually USING that database for anything, we have a problem.
更新:我们给了在EF迁移中使用 Fluent Migrator ,并且很高兴。这是更快,甚至计算事实,我们必须写一些事情两次(一次为EF对象,一次为迁移),它没有在这个问题中讨论的问题。
UPDATE: We gave up on EF Migrations and are using Fluent Migrator instead, and are happy. It's much, much faster, even counting the fact that we have to write some things twice (once for the EF object and once for the Migration), and it doesn't have the problems discussed in this question.
推荐答案
添加迁移
检查数据库的存在并与 __迁移历史
表。正如@Anders Abel所说,它用于调查挂起的迁移,并用于选择以前的模型,以便实际找到发生了什么变化 - 如果将显式迁移添加到启用自动迁移的解决方案中,这一点尤为重要。
Add-Migration
checks existence of the database and interacts with __MigrationHistory
table. As @Anders Abel mentioned it is used for investigating pending migrations and also for selecting previous model to actually find what has changed - this is especially important if you add explicit migration into solution where automatic migrations are enabled.
这篇关于为什么Entity Framework的EF迁移添加迁移步骤需要数据库连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!