Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add-Migration NullReferenceException using App.config. Failing to find config #22205

Closed
TimMcGilly opened this issue Aug 24, 2020 · 5 comments · Fixed by #26752
Closed

Add-Migration NullReferenceException using App.config. Failing to find config #22205

TimMcGilly opened this issue Aug 24, 2020 · 5 comments · Fixed by #26752
Assignees
Labels
area-tools closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported good first issue This issue should be relatively straightforward to fix. type-bug
Milestone

Comments

@TimMcGilly
Copy link

TimMcGilly commented Aug 24, 2020

I have 3 projects: a DataAccess(.net standard) with dbcontext, a UI project (wpf .net core startup project) and model project (.net standard).
I have a file called App.config in both UI and DataAccess projects with this code

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="ScreenTimeTrackerDb"
         connectionString="Server=.\SQLExpress;Database=ScreenTimeTracker;Trusted_Connection=True;" />
  </connectionStrings>
</configuration>

I am loading the configuration with System.Configuration and this override

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["ScreenTimeTrackerDb"].ConnectionString);
        }

When running Add-Migration it throws a null-reference exception due to not finding configuration.

System.NullReferenceException: Object reference not set to an instance of an object.
   at ScreenTimeTracker.DataAccess.ScreenTimeTrackerDbContex.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in C:\Users\timmc\source\repos\ScreenTimeTracker\ScreenTimeTracker.DataAccess\ScreenTimeTrackerDbContex.cs:line 13
   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)

This is run from within visual studio from the Package Managed Console with Default project set to DataAccess and the startup project set to UI.

Using

var configfile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var path = configfile.FilePath;
            throw new System.Exception(path);

it appears it is attempting to open

System.Exception: C:\Users\timmc\source\repos\ScreenTimeTracker\ScreenTimeTracker.UI\bin\Debug\netcoreapp3.1\ef.dll.config` 
whereas the configuration files produced my project are
`ScreenTimeTracker.DataAccess.dll.config`
`ScreenTimeTracker.UI.dll.config`.

Changing the App.config Copy to Output to Copy Always does not resolve the issue.

Further technical details

EF Core version: 3.1.7
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1, .Net Standard 2.1
Operating system:
IDE: Visual Studio 2019 16.7.2

@ajcvickers
Copy link
Member

@bricelam to find dupe.

@bricelam
Copy link
Contributor

Blocked on dotnet/runtime#931

@walter-tudorin-ross-rlb
Copy link

walter-tudorin-ross-rlb commented Apr 1, 2021

Experiencing the same issue as described by TimMcGilly. Any progress on this?
Using

  • EF 5.0.4
  • Winforms App with NET 5
  • Separate Assembly for DbContext

@bricelam bricelam added consider-for-next-release good first issue This issue should be relatively straightforward to fix. and removed blocked area-external labels Sep 16, 2021
@ajcvickers ajcvickers modified the milestones: Backlog, 7.0.0 Oct 25, 2021
@skrabbe001
Copy link

skrabbe001 commented Nov 16, 2021

What is the status on this, i.e. is there a deadline for when this will be fixed? And for which versions of dotnet will it be fixed? I have had the same problem for approximately a year, but didn't report it.

@ajcvickers
Copy link
Member

@skrabbe001 This is currently planned for EF Core 7.0, as indicated by the milestone. This will be released with .NET 7. See the planning process for more information on how we plan what gets fixed in a release.

bricelam added a commit to bricelam/efcore that referenced this issue Nov 18, 2021
@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Nov 18, 2021
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-preview1 Feb 14, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-preview1, 7.0.0 Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-tools closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported good first issue This issue should be relatively straightforward to fix. type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants