添加迁移时用于提取数据库连接字符串时,ConfigurationManager返回null [英] ConfigurationManager return null when using to extract database connection string when add migration

查看:54
本文介绍了添加迁移时用于提取数据库连接字符串时,ConfigurationManager返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.NET Core控制台应用程序中使用Entity Framework Core.我将连接字符串存储在 App.config 文件中,并在上下文类中使用 ConfigurationManager 来访问连接字符串.

I am using Entity Framework Core in my .NET Core console app. I store my connection string in the App.config file and use ConfigurationManager in context class to access the connection string.

当我想向项目中添加新的迁移时,出现以下错误:

When I want to add new migration to my project I get the following error:

System.NullReferenceException:对象引用未设置为对象的实例.

System.NullReferenceException: Object reference not set to an instance of an object.

在C:\ .. \ Context.cs:第12行的EF_tut.Context.OnConfiguring(DbContextOptionsBuilder optionsBuilder)中
在Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
在Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService [TService](IInfrastructure 1访问器)
在Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func
1工厂)在Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)在Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(字符串名称,字符串outputDir,字符串contextType)在Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(字符串名称,字符串outputDir,字符串contextType)在Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase中.<> c__DisplayClass3_0`1.b__0()在Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(操作动作)处

at EF_tut.Context.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in C:\..\Context.cs:line 12
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure1 accessor)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func
1 factory) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

对象引用未设置为对象的实例.

Object reference not set to an instance of an object.

这是我的 App.config 文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
         <add name="EfTutDb" 
              connectionString="Data Source=.;Initial Catalog=EF_tut;Integrated Security=True;"/>
    </connectionStrings>
</configuration>

这是我的上下文类:

class Context : DbContext
{
    public DbSet<Student> Students { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["EfTutDb"].ConnectionString); // Line 12.
    }
}

当我尝试使用main方法中的 ConfigurationManger 获取连接字符串时,我得到了连接字符串,但是当我添加迁移时,我得到了Null Reference错误.

When I try get connection string using ConfigurationManger in the main method, I get the connection string but when I add migration, I get Null Reference error.

推荐答案

也许这是一种解决方法,但是可以使用:

Maybe it's a bit workaround.. but this will work:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var configuration = new ConfigurationBuilder()
            .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
            .AddXmlFile("App.config")
            .Build(); 

        optionsBuilder.UseSqlServer(configuration.GetValue<string>("connectionStrings:add:EfTutDb:connectionString"));
    }
}

这篇关于添加迁移时用于提取数据库连接字符串时,ConfigurationManager返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆