如何添加IDesignTimeDbContextFactory的实现以添加到.Net core 2.0应用程序的迁移?

时间:2017-08-13 02:55:10

标签: asp.net asp.net-mvc entity-framework asp.net-core-2.0

我正在尝试从.NET Core 2.0 MVC应用程序的包管理器控制台运行Add-Migration InitialCreate命令。在查看所有可能的来源之后仍然无法解决错误描述的问题:

PM> Add-Migration InitialCreate
  

调用方法时遇到错误&#39; BuildWebHost&#39;课程&#39;课程&#39;。继续没有应用程序服务提供商。错误:找不到方法:&#39; System.Collections.Generic.Dictionary 2<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.ConcurrentDictionary 2.GetOrAdd(TKey键,Func 2 valueFactory) at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options) at MvcMovie.Models.MvcMovieContext..ctor(DbContextOptions 1个选项)在C:\ Users \ Neha \ source \ repos \中MvcMovie \ MvcMovie \ Data \ MvcMovieContext.cs:第12行          在MvcMovie.ToDoContextFactory.CreateDbContext(String [] args)中的C:\ Users \ Neha \ source \ repos \ MvcMovie \ MvcMovie \ ToDoContextFactory.cs:第17行          在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.b__0()          在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)

我的Program.cs看起来像:

 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();
}

还添加了

的实现
  

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);
    }
}

有人可以帮助我使用描述性的逐步程序将模型和实体框架工具添加到.NET Core2.0应用程序。

3 个答案:

答案 0 :(得分:9)

您需要更改课程

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

此类changed使用dotnet core 2.0.0

答案 1 :(得分:2)

有同样的问题。 它发生在我将Microsoft.AspNetCore.All从2.0.0-preview2-final更新到2.0.0之后。猜测它失败的原因是我安装了.NET Core Preview 2运行时。

EFCore也得到了更新。所以请确保你有相同的nuget和运行时版本

更新:现在,.NET Core运行时和SDK 2.0已发布https://www.microsoft.com/net/download/core。安装后,您可以安全地将软件包更新到2.0.0

答案 2 :(得分:0)

我有同样的问题

我的解决方案包含一个控制台应用程序和一个包含dbcontext代码的api应用程序。

我已将启动项目设置为控制台应用程序。当我将启动项目更改为api应用程序时,错误消失了。