EF Core DbContext Connection - Could Not Load File or Assembly
We have just begun migrating all of our applications from .NET Framework (v4.6.2) to .NET Core (v10) and have made the first attempt at connecting LINQPad back up to the first application, but I am receiving the following:
Error Testing Custom EF Core Connection: Could not load file or assembly 'Microsoft.Extensions.Logging.Abstractions, Version=10.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
We are using the portable version of LINQPad 9 (v9.6.6 (X64)_, licensed as Premium Edition. LINQPad9-x64.exe copied and renamed to LPRun9.exe as per the !!info for xcopy deployments.txt file.
I mention the portable version because this might matter. We're in the process of getting approval to get LINQPad 9 in Software Center for download/install, but getting new software approvals is like moving a mountain. So we're trying the non-installed version at the moment.
Moving on...
The Microsoft.Extensions.Logging.Abstractions package is a transitive dependency of our projects/solution. When working through the "EF Core DbContext Connection" dialog, it is not listed in the NuGet packages that LINQPad auto-finds that it says it needs to download.
I started-from-scratch:
1. Closed LINQPad
2. Cleared any locally-retained NuGet packages by clicking the "Clear NuGet local resources" button in Visual Studio's options
3. Closed Visual Studio
4. Deleted all bin and obj files for each project in our solution (we have a "Web" project for MVC/WebAPI and we have a "Core" project for all of the business logic)
5. Deleted the packages folder (our solution is setup for central package management)
6. Cleared all globally-retained NuGet packages with dotnet nuget locals global-packages --clear; verified %USERPROFILE%/.nuget was empty
7. Launched Visual Studio, opened our solution and ran a "Rebuild Solution"
8. Opened our portable version of LINQPad -> Add Connection... -> Entity Framework Core (3.x -> 10.x)
9. Clicked "Browse" for the "Path to Custom Assembly" field and selected our "Core" project's DLL (where EF is defined/used); the "Confirm Package Download" dialog appears, in which Microsoft.Extensions.Logging.Abstractions is not listed
10. Click "Yes" to download missing packages; and once completed, confirmed that the %USERPROFILE%\.nuget\packages folder is created and filled with the same packages listed in the dialog; Microsoft.Extensions.Logging.Abstractions still missing
11. Back in the "EF Core DbContext Connections" dialog, our DbContext has been automatically found and we simply click "OK" on the dialog that informed us of this
12. We indeed have a (sole) constructor that accepts DbContextOptions, so this (the only selectable) option is selected
13. We select the Microsoft.EntityFrameworkCore.SqlServer provider and enter our connection string
14. After entering a friendly name for the connection, we test the connection... and we get the "could not load" message at the top of this post
Figuring LINQPad is complaining that the DLL can't be found in the global directory for NuGet packages, I figured I'd use LINQPad to force its existence:
1. I closed the connection-creation dialog and pressed F4 while in a script editor window
2. I clicked the "Add NuGet..." button
3. In the "Search online" field, I searched for Microsoft.Extensions.Logging.Abstractions and used the "Add to Script" dropdown to specify that v10.0.0 be added
4. I verified that the %USERPROFILE%\.nuget\packages\Microsoft.Extensions.Logging.Abstractions folder appeared with a fully-filled 10.0.0 folder inside
5. I performed steps 3 and 4 again for v10.0.5, since our projects are up-to-date with pulling in v10.0.5 of Microsoft's NuGet packages
6. I closed LINQPad, opened it again and tried again to add the connection using all the same steps above and I still get the "could not load" message again
Does anyone have any ideas on why this transitive dependency is not being found by LINQPad? It may not even be the only one as it could be that this is just the first one we're encountering as LINQPad tries to resolve needed DLLs.
Answers
-
Do you have any NuGet configuration files that may override it? For "portable" deployments, you would include dependencies and whatnot in the LINQPad directory if I remember correctly. I wonder if that includes the default
.nugetdirectory.Perhaps you could include a
NuGetConfig.xmlfile to specify the paths? -
The fact that you're using the portable version of LINQPad should make no difference.
Does your EF Core project target Microsoft.NET.Sdk.Web by any chance? And if so, does it work if you target Microsoft.NET.Sdk instead?
-
If you're unable to retarget it to Microsoft.NET.Sdk, try the following and let me know if makes any difference:
- From a new script (with no connection selected) press F4 and click Add(Browse) and browse to your data context DLL
- Try instantiating your DbContext and dumping it
- If you get the same error, press F4 > Advanced, and click "Reference ASP.NET Core"
- Tell me whether your script now runs correctly.
-
@JeffMercado and @JoeAlbahari I apologize for the delay. Things have been quite hectic at work. I promise to return soon and leave responses to your posts very soon.
-
I don't believe so. All of our project-level
nuget.configfiles, to include the project I'm connecting LINQPad to, have the following configuration:nuget.config:<?xml version="1.0" encoding="utf-8"?> <configuration> <config> <add key="globalPackagesFolder" value="./packages" /> </config> <packageSources> <clear /> <add key="NAVRES Nexus (v3)" value="https://nexus.navres.dev/repository/nuget-all-v3/index.json" protocolVersion="3" /> </packageSources> </configuration>We proxy the NuGet repo via our on-prem Sonatype Nexus Repository Manager instance. This was one of the first things I had to configure with the portable version. From Tools -> Settings -> NuGet Settings -> Configure NuGet Sources, I have our Nexus instance registered. Oddly, you can't delete the default NuGet 3 source, so I simply have it marked inactive.
Per the link you provided (https://www.linqpad.net/PortableDeployment.aspx - thank you), the changes made via the GUI of the portable LINQPad instance are being writting to a
NuGetSources.xmllocated in the directory from which the portable executable was run.This is juxtaposed with two other LINQPad directories (
%USERPROFILE%/AppData/Local/LINQPadand%USERPROFILE%/AppData/Roaming/LINQPad), of whichLocaldoes NOT contain eitherNuGetConfig.xmlnorNuGetSources.xml, whileRemotecontains both. However, the two files inRemotelook to be default implementations:NuGetConfig.xml:<?xml version="1.0" encoding="utf-8"?> <NuGetOptions xmlns="http://schemas.datacontract.org/2004/07/LINQPad"> <LicensesToAutoAccept></LicensesToAutoAccept> <PackagesToAutoDownload></PackagesToAutoDownload> </NuGetOptions>
NuGetSources.xml:<?xml version="1.0" encoding="utf-8"?> <NuGetSources> <Source Name="(default)" /> </NuGetSources>
I do not have a
NuGetConfig.xmlwithin the directory that contains the portable executable - so I don't think anything is being overruled. Could you give me an example of what you're thinking?
First off, brother... we'd be L-O-S-T without LINQPad. Our use of the tool over the years has, far and beyond been worth the investment we've put into the licenses. Thank you, thank you, THANK YOU!!!
Reply 1:
I agree that the portable instance should not be the culprit - just wanted to mention that so we're all on the same page. Indeed... the
Webproject (from my initial post, we haveCoreandWeb) targetsMicrosoft.NET.Sdk.Web.If I change this to
Microsoft.NET.Sdk, a bunch of ASP.NET-relatedusingstatements need to be added - makes sense, got it and no big deal. However, we lose the the ASP.NET's Configuration system and its capability to layerappsettings.{environment}.jsonconfigurations (a bit of a deal breaker for us).HOWEVER...... after adding all the (now needed) ASP.NET-related
usingstatements and getting the code to build, LINQPad does successfully 1) recognize it needs theMicrosoft.Extensions.Logging.Abstractionspackage, and 2) LINQPad brings it down. AND... I am able to get a connection in LINQPad to test successfully against the application's DbContext.So we're in a bit of a Catch-22 here.

Reply 2:
Requested attempt 1:
Attempted and got:
Message: Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=10.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified."
Stack trace:
at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options) at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options) at Reserve.Locker.Common.Database.Auditing.Legacy.CommonDbContext..ctor(DbContextOptions options) at Reserve.Locker.Common.Database.Auditing.Legacy.AuditedDbContext..ctor(DbContextOptions options, IAuditUsernameProvider auditUsernameProvider) at Reserve.Locker.TNRSubscription.Core.Data.Database.AppDbContext..ctor(DbContextOptions`1 options) in C:\Source\Reserve.Locker.TNRSubscription\src\Reserve.Locker.TNRSubscription.Core\Data\Database\AppDbContext.cs, line 24 at UserQuery.Main(), line 2
Requested attempt 2:
I can successfully instantiate and dump, but of course... I can't expand any data collections because of the following. Just not sure if you were expecting this or not.
Message: No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.
Stack trace:
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, DbContextOptions contextOptions, DbContext context) at Microsoft.EntityFrameworkCore.DbContext.get_ContextServices() at Microsoft.EntityFrameworkCore.DbContext.get_Model() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.CheckState() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Collections.IEnumerable.GetEnumerator()
-
Thanks - that's the info I need. There will be a fix in the next build.
-
This should now be fixed in 9.8.1:
https://www.linqpad.net/linqpad9.aspx#betaThere's a new checkbox in the connection dialog to indicate that your project targets ASP.NET.
Let me know how you get on.
