如何添加 IDesignTimeDbContextFactory 的实现以将迁移添加到 .Net core 2.0 应用程序?
How to add an implementation of IDesignTimeDbContextFactory to add migration to .Net core 2.0 application?
我正在尝试 运行 从 .NET Core 2.0 MVC 应用程序的包管理器控制台添加迁移 InitialCreate 命令。查看所有可能的来源后仍然无法解决问题,错误描述为:
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.ConcurrentDictionary
2.GetOrAdd(TKey key, Func2 valueFactory)
at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options)
at MvcMovie.Models.MvcMovieContext..ctor(DbContextOptions
1 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_0
1.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)
我的 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);
}
}
有人可以通过描述性的分步过程帮助我将模型和 Entity Framework 工具添加到 .NET Core2.0 应用程序。
有同样的问题。
它发生在我将 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
您需要更改您的 class 程序
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
此 class changed 与 dotnet 核心 2.0.0
我遇到了同样的问题
我的解决方案包含一个控制台应用程序和一个包含 dbcontext 代码的 api 应用程序。
我已将启动项目设置为控制台应用程序。当我将启动项目更改为 api 应用程序时,错误消失了。
我正在尝试 运行 从 .NET Core 2.0 MVC 应用程序的包管理器控制台添加迁移 InitialCreate 命令。查看所有可能的来源后仍然无法解决问题,错误描述为:
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()'. 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 key, Func2 valueFactory) at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options) at MvcMovie.Models.MvcMovieContext..ctor(DbContextOptions
1 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_0
1.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)
我的 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);
}
}
有人可以通过描述性的分步过程帮助我将模型和 Entity Framework 工具添加到 .NET Core2.0 应用程序。
有同样的问题。 它发生在我将 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
您需要更改您的 class 程序
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
此 class changed 与 dotnet 核心 2.0.0
我遇到了同样的问题
我的解决方案包含一个控制台应用程序和一个包含 dbcontext 代码的 api 应用程序。
我已将启动项目设置为控制台应用程序。当我将启动项目更改为 api 应用程序时,错误消失了。