MigrateDatabaseToLatestVersion useSuppliedContext = false 的目的是什么?
What is the purpose of MigrateDatabaseToLatestVersion useSuppliedContext = false?
我最近 运行 喜欢的东西。
我有一个动态生成连接字符串的项目,我正尝试在包装这些字符串的上下文中使用 MigrateDatabaseToLatestVersion
。每次我这样做时,我都会看到我的动态数据库没有被创建,而是我的默认构造函数连接字符串(用于测试)上的数据库一遍又一遍地迁移。
在深入研究 EF 迁移源代码后,我发现 MigrateDatabaseToLatestVersion
有一个构造函数
// Summary:
// Initializes a new instance of the MigrateDatabaseToLatestVersion class specifying
// whether to use the connection information from the context that triggered initialization
// to perform the migration.
//
// Parameters:
// useSuppliedContext:
// If set to true the initializer is run using the connection information from the
// context that triggered initialization. Otherwise, the connection information
// will be taken from a context constructed using the default constructor or registered
// factory if applicable.
public MigrateDatabaseToLatestVersion(bool useSuppliedContext);
不是轻率,但您想要迁移不是正在迁移的上下文的原因是什么?为什么这是默认值?有没有人对这里的想法有任何见解?
我想自己知道这个问题的答案。我不知道为什么上下文是这样设计的。但是,我可以大胆猜测为什么当前默认值为 useSuppliedContext=false
.
我反编译了 EntityFramework 的第一个版本以包含迁移支持,EntityFramework-4.3.0,因为我怀疑默认行为是为了向后兼容。我查看了 MigrateDatabaseToLatestVersion
中 IDatabaseInitializer<TContext>.InitializeDatabase(TContext context)
的反编译实现。你猜怎么着?在 EntityFramework-4.3.0 中,该方法的 context
参数被完全忽略。所以它不可能响应显式提供的连接 parameters/settings 因为这些只能通过 context
变量访问。
似乎在 EntityFramework-6.1.1 中添加了对尊重 context
的支持。在此之前,您唯一的选择是将连接字符串传递给 MigrateDatabaseToLatestVersion
的构造函数。我认为这会阻止您在同一进程中对不同的后端使用相同的 DbContext
类型。我敢打赌,如果默认情况下启用尊重上下文(并且行为正确,IMO)的新功能将不会被 EntityFramework 接受,因为这会改变稳定项目可能依赖的行为,否则会阻止项目从采用它。
确切的推理实际上在 commit 777a7a77a740c75d1828eb53332ab3d31ebbcfa3 by Rowan Miller 中作为评论给出:
Also swapping the new useSuppliedContext parameter on MigrateDatabaseToLatestVersion`.cs to be false by default since we are going to be shipping this change in a patch release.
我最近 运行 喜欢的东西。
我有一个动态生成连接字符串的项目,我正尝试在包装这些字符串的上下文中使用 MigrateDatabaseToLatestVersion
。每次我这样做时,我都会看到我的动态数据库没有被创建,而是我的默认构造函数连接字符串(用于测试)上的数据库一遍又一遍地迁移。
在深入研究 EF 迁移源代码后,我发现 MigrateDatabaseToLatestVersion
有一个构造函数
// Summary:
// Initializes a new instance of the MigrateDatabaseToLatestVersion class specifying
// whether to use the connection information from the context that triggered initialization
// to perform the migration.
//
// Parameters:
// useSuppliedContext:
// If set to true the initializer is run using the connection information from the
// context that triggered initialization. Otherwise, the connection information
// will be taken from a context constructed using the default constructor or registered
// factory if applicable.
public MigrateDatabaseToLatestVersion(bool useSuppliedContext);
不是轻率,但您想要迁移不是正在迁移的上下文的原因是什么?为什么这是默认值?有没有人对这里的想法有任何见解?
我想自己知道这个问题的答案。我不知道为什么上下文是这样设计的。但是,我可以大胆猜测为什么当前默认值为 useSuppliedContext=false
.
我反编译了 EntityFramework 的第一个版本以包含迁移支持,EntityFramework-4.3.0,因为我怀疑默认行为是为了向后兼容。我查看了 MigrateDatabaseToLatestVersion
中 IDatabaseInitializer<TContext>.InitializeDatabase(TContext context)
的反编译实现。你猜怎么着?在 EntityFramework-4.3.0 中,该方法的 context
参数被完全忽略。所以它不可能响应显式提供的连接 parameters/settings 因为这些只能通过 context
变量访问。
似乎在 EntityFramework-6.1.1 中添加了对尊重 context
的支持。在此之前,您唯一的选择是将连接字符串传递给 MigrateDatabaseToLatestVersion
的构造函数。我认为这会阻止您在同一进程中对不同的后端使用相同的 DbContext
类型。我敢打赌,如果默认情况下启用尊重上下文(并且行为正确,IMO)的新功能将不会被 EntityFramework 接受,因为这会改变稳定项目可能依赖的行为,否则会阻止项目从采用它。
确切的推理实际上在 commit 777a7a77a740c75d1828eb53332ab3d31ebbcfa3 by Rowan Miller 中作为评论给出:
Also swapping the new useSuppliedContext parameter on MigrateDatabaseToLatestVersion`.cs to be false by default since we are going to be shipping this change in a patch release.