简体   繁体   English

带实体框架的ASP.NET Core-代码优先迁移未读取我的环境变量

[英]ASP.NET Core with Entity Framework - code first migrations aren't reading my environment variables

I have an ASP.NET Core 2 Web API using Entity Framework code first. 我有一个首先使用实体​​框架代码的ASP.NET Core 2 Web API。 My DbContext is not in the same assembly as the API itself so to allow me to use the Microsoft.EntityFrameworkCore.Tools.DotNet tooling I've added an implementation of IDesignTimeDbContextFactory<MyDbContext> to the DbContext 's project: 我的DbContext与API本身不在同一程序集中,因此允许我使用Microsoft.EntityFrameworkCore.Tools.DotNet工具我已将IDesignTimeDbContextFactory<MyDbContext>的实现添加到DbContext的项目中:

public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
{
    private static IConfiguration _configuration;

    public static IConfiguration Configuration
    {
        get
        {
            if (_configuration != null) return _configuration;

            _configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{EnvName ?? "Production"}.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();

            return _configuration;
        }
    }

    public MyDbContext CreateDbContext(string[] args)
    {
        var builder = new DbContextOptionsBuilder<MyDbContext>();
        var connectionString = Configuration.GetConnectionString("MyConnString");
        var migrationsAssembly = typeof(MyDbContext).GetTypeInfo().Assembly.GetName().Name;

        builder.UseSqlServer(connectionString, b => b.MigrationsAssembly(migrationsAssembly));

        return new MyDbContext(builder.Options);
    }
}

I don't have an appsettings.json in this project, but I have added a system environment variable with the name ConnectionStrings:MyConnString and the value of my connection string. 我在这个项目中没有appsettings.json ,但是我添加了一个名为ConnectionStrings:MyConnString和连接字符串值的系统环境变量。

When I run a tooling command such as dotnet ef migrations add "Whatever" then I get an error saying connection string cannot be null. 当我运行诸如dotnet ef migrations add "Whatever"类的工具命令时, dotnet ef migrations add "Whatever"那么我会收到一条错误消息,指出连接字符串不能为null。 So I guess the configuration builder is not reading my environment variable. 因此,我猜配置生成器没有读取我的环境变量。 If I replace the variable connectionString with a hard-coded connection string it works fine. 如果我将变量connectionString替换为硬编码的连接字符串,则可以正常工作。

Can anyone tell me how to get this working? 谁能告诉我如何使它工作? Is there an alternative to using environment variables perhaps which still keeps my connection string secret? 除了使用环境变量之外,还有其他方法可以使我的连接字符串保持秘密吗? As far as I'm aware, user secrets can only be used with web projects. 据我所知,用户机密只能用于Web项目。

IDesignTimeDbContextFactory is, as the name implies, only for development . 顾名思义, IDesignTimeDbContextFactory仅用于开发 It doesn't support, or really have any concept of, environments. 它不支持环境,或实际上没有任何环境概念。 It's simply a way to run migrations against a class library, which because it's not a startup project, has no way to actually have the DbContext injected in. 这只是对类库运行迁移的一种方法,由于它不是启动项目,因此实际上DbContext注入DbContext

Long and short, the docs hard-code the connection string for a reason. 总而言之,文档出于某种原因对连接字符串进行了硬编码。 There's no reason to not hard-code it as it can only ever be that one connection string that's used. 没有理由不对其进行硬编码,因为它只能是所使用的一个连接字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC实体框架的代码优先:没有错误,但表未正确填充 - ASP.NET MVC Entity Framework code-first: no errors but tables aren't populated correctly 发布的Asp.net MVC IIS上的实体框架代码优先迁移错误 - Entity Framework Code First Migrations Error on published Asp.net MVC IIS EF Code First迁移替代ASP.NET Core应用程序 - EF Code First migrations alternative for ASP.NET Core application Asp.net核心代码首次进行数据库迁移 - Asp.net core code first database migrations ASP.NET 中的迁移核心代码首先不起作用 - Migrations in ASP.NET Core code-first not working 在没有实体框架和迁移的ASP.NET Core MVC应用程序中使用ASP.NET标识 - Using ASP.NET Identity in an ASP.NET Core MVC application without Entity Framework and Migrations 带有ASP.NET Core的代码优先实体框架:初始迁移似乎希望表已经存在 - Code First Entity Framework with ASP.NET Core: Initial migration seems to expect tables already exist 我的 Entity Framework Core 和 ASP.NET Core MVC 应用程序连接字符串不会自动更新 - My Entity Framework Core & ASP.NET Core MVC app connection string doesn't update automatically 在 Docker 映像中使用 ASP.Net Core 时应用实体框架迁移 - Apply Entity Framework migrations when using ASP.Net Core in a Docker image 如何从Visual Studio Team Services运行ASP.NET Core Entity Framework迁移 - How do I run ASP.NET Core Entity Framework migrations from Visual Studio Team Services
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM