c# - Entity Framework Core 2.0.0 在不同项目中的迁移

标签 c# .net-core entity-framework-core

我有两个项目。

1) .NetCore 控制台应用

  • Microsoft.EntityFrameworkCore.Tools 2.0.0
  • Microsoft.EntityFrameworkCore.SqlServer 2.0.0
  • Microsoft.Extensions.Configuration.Json 2.0.0

我有一个入口点

class Program
{
    private static IConfiguration Configuration { get; set; }

    static void Main(string[] args)
    {
        IConfigurationRoot configurationRoot = new ConfigurationBuilder()
            .AddJsonFile($"appsettings.json", true, true)
            .Build();

        Configuration = configurationRoot;

        // create service collection
        var serviceCollection = new ServiceCollection();
        ConfigureServices(serviceCollection);

        // create service provider
        serviceCollection.BuildServiceProvider();
    }

    private static void ConfigureServices(ServiceCollection serviceCollection)
    {
        string connectionString = Configuration.GetConnectionString("DefaultConnection");
        serviceCollection.AddDbContext<MyDbContext>(options => options.UseSqlServer(connectionString));
    }
}

和一个 appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "****"
  }
}

2) .NetStandard 库(包含 DbContext)

  • Microsoft.EntityFrameworkCore 2.0.0
  • Microsoft.EntityFrameworkCore.Tools 2.0.0
  • Microsoft.EntityFrameworkCore.SqlServer 2.0.0

我希望能够使用 dotnet ef migrations add [NAME] 创建迁移脚本我已经知道,如果我在 lib 文件夹中运行命令,我可以定位一个启动项目来运行迁移。

D:\Lib> dotnet ef migrations add InitialCreate -s ..\ConsoleApp\

我设法通过在与实现 IDesignTimeDbContextFactory<MyDbContext> 的 MyDbContext 相同的文件夹中添加一个类来实现此功能我可以添加一个连接字符串,它运行完美。

public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
{
    public MyDbContext CreateDbContext(string[] args)
    {
        DbContextOptionsBuilder<MyDbContext> builder = new DbContextOptionsBuilder<MyDbContext>();
        builder.UseSqlServer("****", optionsBuilder => optionsBuilder.MigrationsAssembly(typeof(MyDbContext).GetTypeInfo().Assembly.GetName().Name));
        return new MyDbContext(builder.Options);
    }
}

The problem I have is I want to use the connection string from within the console app's appsettings.json.

这可能吗?我该怎么做?

提前致谢。

最佳答案

好吧,我想我有一个解决方案,虽然我不确定我是否满意。

我的 program.cs 现在是:

class Program
{
    static void Main(string[] args)
    {
    }
}

我移动了实现 IDesignTimeDbContextFactory<MyDbContext> 的类从带有 MyDbContext 的项目到控制台应用程序,它现在看起来像:

public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
{
    public MyDbContext CreateDbContext(string[] args)
    {
        IConfiguration configuration = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", true, true)
            .Build();

        string connectionString = configuration.GetConnectionString("DefaultConnection");

        DbContextOptionsBuilder<MyDbContext> builder = new DbContextOptionsBuilder<MyDbContext>();
        builder.UseSqlServer(connectionString,
            optionsBuilder => optionsBuilder.MigrationsAssembly(typeof(MyDbContext).GetTypeInfo().Assembly.GetName().Name));
        return new MyDbContext(builder.Options);
    }
}

所以控制台应用程序是:

  • 程序.cs
  • MyDbContextFactory.cs

我运行的命令还是

D:\Lib> dotnet ef migrations add InitialCreate -s ..\ConsoleApp\

我无法删除对 Microsoft.EntityFrameworkCore.SqlServer 的引用,因为迁移脚本已保存到 lib 项目并引用了库。如果我将迁移脚本转到不同的文件夹,这意味着我可以删除依赖项,但我现在可以接受。

关于c# - Entity Framework Core 2.0.0 在不同项目中的迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45979212/

相关文章:

c# - Asp.Net - 无法创建类型的常量值。在此上下文中仅支持原始类型或枚举类型

c# - WebRequest 在 ASP.NET 应用程序中失败并显示 "414 Request URI too long"

c# - EntityFramework Core 2.2 到 3.1.1 出现错误

c# - Entity Framework Core - 不使用主键的两个表之间的关系

c# - 为所有实体设置级联删除

c# - 我可以使用 Xamarin 为 Windows 7 编写应用程序吗?

c# - 每次调用时更改行为的最小起订量语法是什么?

cookies - 禁用 .AspNetCore.Antiforgery Cookie

.net - 不同的运行时版本和新的 .NET CLI

c# - Entity Framework Core 2.1 无法正确翻译查询