Hi,
Welcome to our Microsoft Q&A platform!
You need to make sure that the programName.exe.config file is generated in the bin directory so that ConfigurationManager can read it.
Thanks.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Upon facing the said issue even followed the simple walkthrough at Connection Strings - EF Core but found out that while hardcoding the connectionstring as:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;");
}
leads to success, using the recommended approach of inserting an App.config file like:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="BloggingDatabase"
connectionString="Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
and updating the main code to:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["BloggingDatabase"].ConnectionString);
}
is not successful, and following error being faced while carrying out migration:
System.NullReferenceException: Object reference not set to an instance of an object.
at Intro.BloggingContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in C:\<...FileLocation...>\AppDbContext.cs:line 14
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService[TService](IInfrastructure`1 accessor)
at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 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.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.
What could be the reason behind the same?
Strange enough! Upon using the index technique i.e. like:
optionsBuilder.UseSqlServer(System.Configuration.ConfigurationManager.ConnectionStrings[0].ConnectionString);
is successful but regretfully doesnot allow to update or remove-migration on the following complaint:
System.ArgumentException: Invalid value for key 'attachdbfilename'
Thanks in advance for your reviewing and replying.
Hi,
Welcome to our Microsoft Q&A platform!
You need to make sure that the programName.exe.config file is generated in the bin directory so that ConfigurationManager can read it.
Thanks.
.NET Core applications do not honor app.config at all, but use different ways for configuration,
https://learn.microsoft.com/en-us/dotnet/core/run-time-config/
DI the connectionstring to the DBcontext in using EF Core from the appsettings.json file.
https://marcominerva.wordpress.com/2019/03/19/using-entity-framework-core-with-wpf-on-net-core-3-0/
https://learn.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext
HTH