9

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.Dictionary2<System.String,System.Object> Microsoft.Extensions.Configuration.IConfigurationBuilder.get_Properties()'. 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.GetOrAdd(TKey key, Func2 valueFactory) at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options) at MvcMovie.Models.MvcMovieContext..ctor(DbContextOptions1 options) in C:\Users\Neha\source\repos\MvcMovie\MvcMovie\Data\MvcMovieContext.cs:line 12 at MvcMovie.ToDoContextFactory.CreateDbContext(String[] args) in C:\Users\Neha\source\repos\MvcMovie\MvcMovie\ToDoContextFactory.cs:line 17 at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func1 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_01.b__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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Neha
  • 753
  • 1
  • 5
  • 18
  • 1
    How could this connection string be read from app.settings.json so that is not hardcoded here again? – borisdj Aug 17 '17 at 11:45
  • Found Solutution for reading from .json: https://stackoverflow.com/questions/45796776/get-connectionstring-from-appsettings-json-instead-of-being-hardcoded-in-net-co – borisdj Aug 24 '17 at 12:24

3 Answers3

9

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

Thiago Silva
  • 437
  • 2
  • 10
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

Sergey Romanchuk
  • 707
  • 8
  • 14
0

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.

Kirsten
  • 15,730
  • 41
  • 179
  • 318