【问题标题】:Entity Framework Core and ASP.NET Core Web API connection string : how to remove the connection string in two places?Entity Framework Core 和 ASP.NET Core Web API 连接字符串:如何删除两个地方的连接字符串?
【发布时间】:2021-09-20 16:29:14
【问题描述】:

我有一个引用 EF Core 项目的 ASP.NET Core Web API 项目。

当我搭建 EF Core 项目时,它会自动在 OnConfiguring 方法中设置连接字符串:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
        optionsBuilder.UseSqlServer("Server=etc;Database=etc;uid;pwd;");
    }
}
    

然后我发现,相当于把连接字符串放到web.config中,就是放到Web API项目的appSettings.json文件中,在StartupConfigureServices方法中读取Web API 项目的类等...

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddSwaggerGen(c => ...);

    var CNN_STR = Configuration.GetConnectionString("DBNameInAppSettingsJson");
    services.AddDbContext<MyDBContext>(options =>
        options.UseSqlServer(CNN_STR));
}

但是现在连接字符串在两个地方!我应该从 EF Core 项目的 OnConfiguring() 方法中删除它吗?如果有,怎么做?

【问题讨论】:

  • 第二个连接字符串仅在 (!optionsBuilder.IsConfigured) 时使用。但它已经在启动时配置了
  • 有同样的问题。创建迁移需要一个,另一个用于正常运行的应用程序。我想出的唯一方法是在 OnConfiguring() 或 IDesignTimeDbContextFactory 中使用 Microsoft.Extensions.Configuration.ConfigurationBuilder 创建 IConfigurationRoot 的实例。这不会删除设置连接字符串的代码,但至少两个地方都从配置中获取相同的连接字符串。
  • @Serge,是的,它已经在 Startup 中配置,但是当代码运行检查时。IsConfigured,它仍然显示为 false,这让我感到惊讶! (这适用于 .NET 5.0)
  • @joedotnot 我刚刚检查了我的 net5 应用程序,isconfigured 是真的。所以也许你需要重新检查你是否正确配置了它。对于迁移,它也使用启动配置。或者只是尝试评论 dbcontext 代码并查看它是否正常工作。
  • 有一个选项可以不包含 onconfiguring 代码。

标签: c# entity-framework-core asp.net-core-webapi connection-string


【解决方案1】:

最好的方法是使用 Startup.cs 中的 ConfigureServices 来使用依赖注入(搜索该术语)。

然后,将已配置的 DbContext 注入您需要的位置(您将在文档中看到更多信息),而不是在需要时手动实例化 DbContext。

https://docs.microsoft.com/en-us/ef/core/dbcontext-configuration/

关于 IsConfigured 仍然是错误的评论,这可能是因为您在某处手动实例化 DbContext,而不是注入它(依赖注入)。如果您注入它,您将从 ConfigureServices 获得配置的版本。如果你手动实例化它,你会得到一个未配置的版本。

如果您想在控制器中配置 DbContext 的版本,请使用以下内容:

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddSwaggerGen(c => ...);

    var CNN_STR = Configuration.GetConnectionString("DBNameInAppSettingsJson");
    services.AddDbContext<MyDBContext>(options =>
        options.UseSqlServer(CNN_STR));
}

控制器

public class MyController : ControllerBase
{
    private readonly MyDbContext _Context;
    
    public MyController(
        MyDbContext context)
    {
        // The parameter 'context' is provided by ASP Dependency Injection
        // ... It will be the configured version from above startup.cs
        _Context = context;
    }

    public async Task DoSomething()
    {
        // Use the configured context
        await _Context.Widgets.AddAsync(new Widget());
    }
}

【讨论】:

  • “这可能是因为您在某处手动实例化 DbContext”,这正是我正在做的,就像在 Full Fx 4.x 中它自动从 web.config 注入一样。我试用后会回来接受您的回答。
  • 我错过了控制器构造函数,因为当我使用它的添加控制器向导时 VS2019 不会自动创建一个,因此我不知道这些可能性。感谢这个答案,我现在可以删除 EF 项目中的 OnConfiguring(...) 方法。
猜你喜欢
  • 1970-01-01
  • 2018-05-08
  • 1970-01-01
  • 1970-01-01
  • 2018-05-23
  • 1970-01-01
  • 2022-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多