I am trying to run Add-Migration InitialCreate command from package manager console from a .NET Core 2.0 MVC application. After looking at all possible sources still not able to resolve the issue with error description as :
PM> Add-Migration InitialCreate
An error occurred while calling method ‘BuildWebHost’ on class ‘Program’. Continuing without the application service provider. Error: Method not found: ‘System.Collections.Generic.Dictionary
2<System.String,System.Object> Microsoft.Extensions.Configuration.IConfigurationBuilder.get_Properties()'.2.GetOrAdd(TKey key, Func
System.IO.FileLoadException: Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.2.1, 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.2.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.TryAddCoreServices()
at Microsoft.Extensions.DependencyInjection.SqlServerServiceCollectionExtensions.AddEntityFrameworkSqlServer(IServiceCollection serviceCollection)
at Microsoft.EntityFrameworkCore.Infrastructure.Internal.SqlServerOptionsExtension.ApplyServices(IServiceCollection services)
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.ApplyServices(IDbContextOptions options, ServiceCollection services)
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.<GetOrAdd>b__2(Int64 k)
at System.Collections.Concurrent.ConcurrentDictionary2 valueFactory)1 options) in C:UsersNehasourcereposMvcMovieMvcMovieDataMvcMovieContext.cs:line 12
at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options)
at MvcMovie.Models.MvcMovieContext..ctor(DbContextOptions
at MvcMovie.ToDoContextFactory.CreateDbContext(String[] args) in C:UsersNehasourcereposMvcMovieMvcMovieToDoContextFactory.cs:line 17
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func1 factory)1.b__0()
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
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.2.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
My Program.cs looks like:
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
}
public static IWebHost BuildWebHost(string[] args) =>
new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
}
Also added implementation for
IDesignTimeDbContextFactory
public class ToDoContextFactory : IDesignTimeDbContextFactory<MvcMovieContext>
{
public MvcMovieContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder<MvcMovieContext>();
builder.UseSqlServer("Server=(local);Database=MvcMovieContext;Trusted_Connection=True;MultipleActiveResultSets=true");
return new MvcMovieContext(builder.Options);
}
}
Can someone help me with a descriptive step by step procedure to add model and Entity Framework tools to a .NET Core2.0 App.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You need change your class Program
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
This class changed with dotnet core 2.0.0
Method 2
Had the same issue.
It happened after i updated Microsoft.AspNetCore.All from 2.0.0-preview2-final to 2.0.0. Guess the reason why it fails is that i have .NET Core Preview 2 runtime installed.
EFCore got updated too. So please, make sure you have same nuget and runtime versions
UPDATE: Now .NET Core runtime and SDK 2.0 released https://www.microsoft.com/net/download/core. After install you can safely update your packages to 2.0.0
Method 3
I had the same issue
My solution contained a console application and an api application which contained the dbcontext code.
I had set the start up project to the console application. When I changed the start up project to the api application the error dissapeared.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0