ConfigurationManager在添加迁移时用于提取数据库连接字符串时返回null [英] ConfigurationManager return null when using to extract database connection string when add migration
问题描述
我在我的 .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.
在 EF_tut.Context.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in C:..Context.cs:line 12
在 Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
在 Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure1 accessor)
1 factory)在 Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)在 Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0()在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
在 Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func
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)
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)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func
未将对象引用设置为对象的实例.
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
获取连接字符串时,我得到了连接字符串,但是当我添加迁移时,我得到了空引用错误.
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屋!