When using the cli command "dotnet ef migrations add" from within the root of the database project an exception is thrown relating to 'System.Diagnostics.DiagnosticSource, Version=4.0.1.0'. This happens when running the command using the latest version of the SDK (1.0.1)
There are two work arounds. One is to edit the project file and change the target framework to netcoreapp1.0 (from netcoreapp1.1) and the other is to specify the startup project via the -s switch in the command.
It is possible that in the situation where the dotnet startup project and the database project are split that the -s switch should always be used but it would be better if this was stated explicitly via a user friendly error message rather than an exception being thrown which doesn't really point the user to what needs to be done.
Stack trace:
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..ctor(DbContextOptions options)
at WebAPI.Database.ActivityTimerDbContext..ctor(DbContextOptions`1 options, Boolean doMigrate) in c:\Workspaces\git\ActivityTimer\WebAPI\WebAPI.Database\ActivityTimerDbContext.cs:line 19
at WebAPI.Database.MigrationsContextFactory.Create(DbContextFactoryOptions options) in c:\Workspaces\git\ActivityTimer\WebAPI\WebAPI.Database\MigrationsContextFactory.cs:line 29
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.GetMigrations(String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.GetMigrationsImpl(String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass4_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Exception message:
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)
.NET Command Line Tools (1.0.1)
Product Information:
Version: 1.0.1
Commit SHA-1 hash: 005db40cd1
Runtime Environment:
OS Name: Windows
OS Version: 10.0.15055
OS Platform: Windows
RID: win10-x64
Base Path: C:Program Filesdotnetsdk1.0.1
I am getting this aswell (at first intermittent but now always). Could you please post an example with the -s switch please?
I tried the -s/--startup-project workaround that was suggested and it doesn't appear to have an effect. I'm still getting this exception.
My project structure is as follows
|- Tests
|- App
|- Presentation (startup project)
|- Database
The command I am using when positioned in the Database project directory is:
dotnet ef migrations add Initial --environment Development --startup-project ..\Presentation
My startup project has the following dependencies in the csproj
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="CsvHelper" Version="3.0.0-beta7" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration.Tools" Version="1.1.0-preview4-final" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.AspNetCore.Server.IISIntegration.Tools" Version="1.1.0-preview4-final" />
</ItemGroup>
My database project has the following
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild3-final" />
</ItemGroup>
There is some more information on using the -s / --startup-project switch here:
https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet
I hope that helps.
WHOA!!! @GZidar That's fantastic!!!
Ok. So, I've got a similar project structure. The difference between what you're doing and what I was doing was that I was in the Database project folder (working directory) and setting the startup project to the same folder. What you're doing (and what makes it work) is setting that startup project to _another_ project in a different directory from the Database project.
This is absolutely fantastic!!! You're the BEST! :-)
-Ben
Thanks for that @GZidar
In my situation I have a project for the entities and dbcontext, and another project for the database migrations (which is a console application, however it doesn't have to be) - in essence my database section (as compared to yours) is split into multiple projects.
My "startup" project is completely disjoint and independent from my data layer. I suppose I can add yet another project, which would be a console application for the sole purpose of doing this workaround? This project can live in my data layer as well.
Hi @UmairB.
My startup project is also separate from the data layer in that I have a csproj file in the Database folder and I have a different csproj file in the Presentation folder.
You should be able to reference the startup project from whichever database project you are attempting to migrate. Does that make sense? In fact @benday has written a blog entry describing the work around in more detail which might better illustrate what to do. The link is:
Thanks @benday I certainly appreciate your kind words but I can't take all the credit. I did create this issue with the hope that the exception be replaced with a nice dev friendly message but it was @haraldnagal who found that the -s / --startup-project switch was the way to get around it. :-)
Hi folks,
Thank you so much for this workaround @GZidar. I'm able to run migrations but now when I do dotnet ef database update I'm getting the same "System.IO.FileLoadException" problem. I have my startup project separated from the data layer. Do you have any thoughts? Thanks in advance!
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.b__2(Int64 k)
at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory)
at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options)
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext`1..ctor(DbContextOptions options)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)
I filed a more concise issue with a more isolated workaround: #7889
Dupe of #7889
@GZidar totally support idea to add user friendly error message. The message "Could not load file or assembly" really makes me confused and I naively tried solve it by adding System.Diagnostics.DiagnosticSource dependency.
I spent whole day on it because before this issue I faced another issue cos by default "dotnet new classlib" (in my case database library) create netstandard1.X and this target framework totally doesn't work with migration it produce another error like this. I was really stuck until I realize that I have to use netcoreapp1.X.
We really need user friendly error messages! Otherwise people who get started with .net core spend hours to fix this issues instead to focus on development process.
Most helpful comment
My project structure is as follows
The command I am using when positioned in the Database project directory is:
My startup project has the following dependencies in the csproj
My database project has the following
There is some more information on using the -s / --startup-project switch here:
https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet
I hope that helps.