简体   繁体   English

在类库中执行asp.net核心EF迁移而无需对连接字符串进行硬编码

[英]Executing asp.net core EF migration in a class library without hardcoding connection string

I would like to execute migration in separate environments. 我想在单独的环境中执行迁移。 I'm working with Entity Framework and ASP.NET Core. 我正在使用实体框架和ASP.NET Core。 For now I have this : 现在我有这个:

public class TemporaryDbContextFactory : IDbContextFactory<ApplicationDbContext>
{
    public ApplicationDbContext Create(DbContextFactoryOptions options)
    {
        var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
        builder.UseSqlServer("Data Source=.\\LOCALHOST;Initial Catalog=MyDB;Integrated Security=True;MultipleActiveResultSets=true");

        return new ApplicationDbContext(builder.Options);
    }
}

This code is in a DAL class library who separate the database interactions from the server code. 该代码位于DAL类库中,该库将数据库交互与服务器代码分开。

It's ok to use this when executing migrations but I don't really like doing it like this. 在执行迁移时可以使用此功能,但我真的不喜欢这样做。

The problem here is that I use a local database for development but we also have a development server and a staging server with different connection strings. 这里的问题是,我使用本地数据库进行开发,但是我们也有带有不同连接字符串的开发服务器和登台服务器。 What I can't manage to do is to find a way to not hardcode the connection strings in that class. 我无法做的是找到一种不对此类中的连接字符串进行硬编码的方法。

Is there a way to configure the connection strings for different environments and indicate which to use when executing the migrations ? 有没有一种方法可以为不同的环境配置连接字符串,并指示执行迁移时使用哪个?

Yes, you can use environment variables for this. 是的,您可以为此使用环境变量。

Thats why you have appsettings.<EnvironmentName>.json 这就是为什么要进行appsettings.<EnvironmentName>.json

Configuration in ASP.NET Core 在ASP.NET Core中进行配置
Not bad Microsoft article abount Working with multiple environments 不错的Microsoft文章擅长使用多种环境


FYI FYI
Environment.GetEnvironmentVariable Environment.GetEnvironmentVariable

You need to inject DbContextOptionsBuilder<ApplicationDbContext> into your factory constructor and then configure the DbContext in the main application's Startup.cs 您需要将DbContextOptionsBuilder<ApplicationDbContext>注入到工厂构造函数中,然后在主应用程序的Startup.cs配置DbContext。

public class TemporaryDbContextFactory : IDbContextFactory<ApplicationDbContext>
{
    private readonly DbContextOptionsBuilder<ApplicationDbContext> optionsBuilder;

    public TemporaryDbContextFactory(DbContextOptionsBuilder<ApplicationDbContext> optionsBuilder)
    {
        if(optionsBuilder==null)
            throw new ArgumentNullException(nameof(optionsBuilder));

        this.optionsBuilder = optionsBuiler;
    }

    public ApplicationDbContext Create(DbContextFactoryOptions options)
    {
        return new ApplicationDbContext(optionsBuilder.Options);
    }
}

And in Startup.cs: 在Startup.cs中:

services.AddDbContext<ApplicationDbContext>(options => {
    options.UseSqlServer(Configuration.GetConnectionString("MyConnectionString"));
});

Configuration.GetConnectionString("MyConnectionString") is shorthand for Configuration["ConnectionStrings:MyConnectionString"] . Configuration.GetConnectionString("MyConnectionString")Configuration["ConnectionStrings:MyConnectionString"]简写。

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

相关问题 EF 核心 - 为没有连接字符串和另一个类库中的数据库创建迁移 - EF core - Creating a migration for a database without a connection string and in another class library EF Core - 创建没有连接字符串的迁移 - EF Core - Create a migration without connection string 如何从ASP.NET Core读取.NET标准类库项目中的连接字符串 - How to read connection string inside .NET Standard Class library project from ASP.NET Core 从类库中读取放置在 ASP.NET Core 中的连接字符串。 数据库优先 - Reading connection string placed in ASP.NET Core from class library. Database First 如何在带有 EF Core 的 ASP.NET Core 中取消应用迁移 - How to unapply a migration in ASP.NET Core with EF Core ASP.NET Core EF Add-Migration 命令不起作用 - ASP.NET Core EF Add-Migration command not working 为 ASP.NET Core MVC & EF Core 解决方案提供 Azure SQL DB 连接字符串 - Supplying Azure SQL DB connection string for ASP.NET Core MVC & EF Core solution 添加.NET 4.5.0 EF6类库作为对ASP.NET Core应用程序的引用 - Adding .NET 4.5.0 EF6 Class Library as a reference to ASP.NET Core Application 将连接字符串从另一个库传递到 .Net Core EF 库 - Pass connection string to .Net Core EF library from another library 如何在单独的类库中使用EF 6.0在ASP.Net核心项目中设置标识 - How to set up Identity in an ASP.Net core project using EF 6.0 in a separate class library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM