A Non-Terrible Workaround for the Dreaded EF Core Migration FileLoadException Diagnostics Problem

Posted · 1 Comment

In my Entity Framework Core Migrations walkthrough, I showed a workaround for an exception when you add a migration.  If you ran “dotnet ef migrations add” on a netcoreapp1.1 project, you’d get the following exception.

System.IO.FileLoadException: Could not load file or assembly ‘System.Diagnostics.DiagnosticSource, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51’. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: ‘System.Diagnostics.DiagnosticSource, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51’
at Microsoft.EntityFrameworkCore.Infrastructure.RelationalServiceCollectionExtensions.AddRelational(IServiceCollection services)
at Microsoft.Extensions.DependencyInjection.SqlServerServiceCollectionExtensions.AddEntityFrameworkSqlServer(IServiceCollection services)
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_1.<GetOrAdd>b__2(Int64 k)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.EntityFrameworkCore.DbContext.InitializeServices()
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
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.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Could not load file or assembly ‘System.Diagnostics.DiagnosticSource, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51’. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

The workaround was to change the framework target for your Entity Framework project from netcoreapp1.1 to netcoreapp1.0.  It’s kind of a terrible work around to have to downgrade your framework target but at the time that I was writing that guide, the .NET Core 1.1 SDK was still in pre-release.  Well, it’s no longer in pre-release and this bug still exists.

The non-terrible workaround comes from Goran Zidar & Harald Nagel via this thread.   In his workaround, you can keep your project targeting netcoreapp1.1 and all you have to do to get around the problem is to add a single argument to your “dotnet ef migrations” command.  To make it work, add a startup project arg (“-s”) and point it to a folder other than the one that you want to add your Entity Framework migration to.

Assuming that your solution looks like the screenshot below and has two non-test projects: EfCoreDemo.Api and EfCoreDemo.WebUi.  The WebUi project is the ASP.NET MVC project.  The EfCoreDemo.Api is where your data context and entities live and where your EF Migrations get stored.

From the command line, “cd” to the EfCoreDemo.Api directory.  Then run “dotnet ef migrations add” with “-s ..\EfCoreDemo.WebUi” as an argument.  IT WORKS!

Once again, giant thanks to Goran Zidar & Harald Nagel for this workaround.

-Ben


One Response to "A Non-Terrible Workaround for the Dreaded EF Core Migration FileLoadException Diagnostics Problem"
 

© 2016 Benjamin Day Consulting, Inc.
Brookline, MA
617-645-0188
info@benday.com | |