首页 > 解决方案 > 如何从自定义 NuGet 包运行 EF 迁移?

问题描述

我有 ac# 类库,它有自己的数据库上下文和实体框架迁移。

我的解决方案包含一个将类库作为项目参考引用的网站。

通过将默认项目设置为我的类库并使用以下命令,我可以从包管理器控制台为 Visual Studio 中的类库应用 EF 迁移:

update-database -StartupProject MyMainWebProj -Context MyClassLibDbContext

我为类库创建了一个私有 NuGet 包,并更新了网站以引用 NuGet 包而不是项目引用。

现在我无法弄清楚如何运行 EF 迁移,因为类库是通过 NuGet 包引用的。

标签: visual-studioentity-frameworknuget-packageentity-framework-migrations

解决方案


您可以在服务配置期间使用 Use*() 方法的构建器对象,如下所述:https ://github.com/jasontaylordev/CleanArchitecture/blob/d0f133ee026aec5cd5856c5592c307b5f20fa8e4/src/Infrastructure/DependencyInjection.cs#L28

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(
        configuration.GetConnectionString("DefaultConnection"),
        b => b.MigrationsAssembly(typeof(SomeClassInTheOtherAssembly).Assembly.FullName)));

推荐阅读