How to add an implementation of IDesignTimeDbContextFactory to add migration to .Net core 2.0 application?

asp.net asp.net-core-2.0 asp.net-mvc entity-framework

Question

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.

1
9
11/8/2017 9:39:42 PM

Accepted Answer

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

9
8/17/2017 12:51:29 AM

Popular Answer

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow