DBContext被第三个paty dll锁定时的EF迁移

时间:2022-10-05 14:54:58

Given a DBContext which resides in an assembly (foo.dll). I do not have direct access to the code for foo.dll, which was developed by a third party. You can assume that the third party has no sight of my database against which I want to run the migrations.

给定一个驻留在程序集中的DBContext(foo.dll)。我无法直接访问由第三方开发的foo.dll代码。您可以假设第三方看不到我想要运行迁移的数据库。

Is it possible for me to perform code first migrations by referencing foo.dll, which I am consuming in my application?

我是否可以通过引用foo.dll来执行代码优先迁移,我在我的应用程序中使用它?

UPDATE

Any solution the third party can implement,with the exception of exposing the dbcontext class or doing the migrations themselves, will also be fine

除暴露dbcontext类或自己进行迁移外,第三方可以实现的任何解决方案也都可以

2 个解决方案

#1


You need to run the migration from a project which references the assembly.

您需要从引用程序集的项目运行迁移。

So, I would suggest you create a separate project for seeding within your solution which references foo.dll.

所以,我建议你在你的解决方案中创建一个单独的项目来引用foo.dll。

Ensure that your database connection string is contained within your web.config or app.config file (You will have to ask the third party the connection string name you should use, or look it up in their documentation if available).

确保您的数据库连接字符串包含在您的web.config或app.config文件中(您必须向第三方询问您应该使用的连接字符串名称,或者在文档中查找(如果可用))。

Next, open package manager and run the following commands (use the fqn of the assembly where applicable):

接下来,打开包管理器并运行以下命令(使用适用的程序集的fqn):

enable-migrations -ContextAssemblyName foo; add-migration "initial"; update-database;

#2


Perhaps foo.dll could add a function that would

也许foo.dll可以添加一个功能

Database.SetInitializer(new MigrateDatabaseToLatestVersion<...,...>());
Database.Initialize(true);

or some variation of that to trigger the migration.

或者一些变化来触发迁移。

#1


You need to run the migration from a project which references the assembly.

您需要从引用程序集的项目运行迁移。

So, I would suggest you create a separate project for seeding within your solution which references foo.dll.

所以,我建议你在你的解决方案中创建一个单独的项目来引用foo.dll。

Ensure that your database connection string is contained within your web.config or app.config file (You will have to ask the third party the connection string name you should use, or look it up in their documentation if available).

确保您的数据库连接字符串包含在您的web.config或app.config文件中(您必须向第三方询问您应该使用的连接字符串名称,或者在文档中查找(如果可用))。

Next, open package manager and run the following commands (use the fqn of the assembly where applicable):

接下来,打开包管理器并运行以下命令(使用适用的程序集的fqn):

enable-migrations -ContextAssemblyName foo; add-migration "initial"; update-database;

#2


Perhaps foo.dll could add a function that would

也许foo.dll可以添加一个功能

Database.SetInitializer(new MigrateDatabaseToLatestVersion<...,...>());
Database.Initialize(true);

or some variation of that to trigger the migration.

或者一些变化来触发迁移。