gpt4 book ai didi

c# - EF 核心无法使用 UseSqlServer 和 SpecFlow 解析环境变量

转载 作者:行者123 更新时间:2023-12-04 07:34:34 25 4
gpt4 key购买 nike

我正在将 BDD 测试项目转换为使用 EF 核心 5.x 和 SpecFlow 3.x + Specflow.Autofac。执行场景时,上下文无法从环境变量中找到连接字符串。
解决方案设置具有以下结构

  • API(项目)

  • appsettings.json(不是应该用于规范流测试的那个)

  • 业务(项目)

  • BusinessRegister.cs

  • 数据(项目)

  • MyContext.cs


  • DataRegister.cs

  • BddTest(项目)

  • appsettings.json(具有用于 mycontext 的 bdd 测试的连接字符串)


  • DependencyResolver.cs


  • 各种场景


  • 在所有场景执行之前注册场景依赖;
    public static class DependencyResolver
    {
    private static IConfiguration config;

    [ScenarioDependencies]
    public static ContainerBuilder CreateContainerBuilder()
    {
    var containerBuilder = new ContainerBuilder();
    if (config == null)
    {
    string fullPath = System.Reflection.Assembly.GetAssembly(typeof(DependencyResolver)).Location;
    string theDirectory = Path.GetDirectoryName(fullPath);

    config = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .SetBasePath(theDirectory)
    .AddEnvironmentVariables()
    .Build();
    }

    containerBuilder.RegisterInstance(config);
    containerBuilder.RegisterTypes(typeof(DependencyResolver).Assembly.GetTypes().Where(t => Attribute.IsDefined(t, typeof(BindingAttribute))).ToArray()).SingleInstance();
    containerBuilder.RegisterModule(new DependencyRegister());
    containerBuilder.RegisterModule(new BusinessRegister());
    containerBuilder.RegisterModule(new DataRegister());
    return containerBuilder;
    }
    }
    DataRegister 寄存器 MyContext ;
    public class DataRegister : Module
    {
    protected override void Load(ContainerBuilder builder)
    {
    builder.RegisterType<MyContext>().AsSelf().InstancePerDependency();
    }
    }
    MyContext它叫 UseSqlServer尝试解析环境变量
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
    optionsBuilder.EnableDetailedErrors();

    if (!optionsBuilder.IsConfigured)
    {
    optionsBuilder.UseSqlServer("Name=ConnectionStrings:MyContext");
    }
    }
    appsettings.json位于 BddTest项目
    {
    "Logging": {
    "LogLevel": {
    "Default": "Information",
    "Microsoft": "Warning",
    "Microsoft.EntityFrameworkCore": "Debug",
    "Microsoft.Hosting.Lifetime": "Information"
    }
    },
    "ConnectionStrings": {
    "MyContext": "--"
    }
    }
    当我尝试运行场景时,应用程序在尝试在 MyContext 内部创建数据库连接时出错。 ;

    System.InvalidOperationException: 'A named connection string was used,but the name 'ConnectionStrings:MyContext' was not found in theapplication's configuration. Note that named connection strings areonly supported when using 'IConfiguration' and a service provider,such as in a typical ASP.NET Core application.


    我如何注册我的 appsettings.json以便它在 Data 内可用项目,使用时可以解决 optionsBuilder.UseSqlServer("Name=ConnectionStrings:MyContext"); ?

    最佳答案

    尝试这个:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
    optionsBuilder.EnableDetailedErrors();

    if (!optionsBuilder.IsConfigured)
    {
    var config = ConfigurationManager.ConnectionStrings["MyContext"];
    var conn = new SqlConnection(config.ConnectionString);

    options.UseSqlServer(conn);
    }
    }

    关于c# - EF 核心无法使用 UseSqlServer 和 SpecFlow 解析环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67798355/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com