WPF和EntityFrameworkCore-添加迁移会产生;没有为此DbContext配置数据库提供程序;
注意我已经阅读了大量看似相似的SO答案,但我已经按照他们的建议做了,所以我不知道WPF是否有一些不同(它们似乎都与ASP.NET有关)。此外,大多数答案与运行时错误有关,而不是与添加迁移时的错误有关 我正在尝试设置一个使用EntityFrameWork Core的.NET Core 3 WPF项目,但在添加迁移时遇到问题。我已经设置了我的上下文如下WPF和EntityFrameworkCore-添加迁移会产生;没有为此DbContext配置数据库提供程序;,wpf,entity-framework,.net-core,Wpf,Entity Framework,.net Core,注意我已经阅读了大量看似相似的SO答案,但我已经按照他们的建议做了,所以我不知道WPF是否有一些不同(它们似乎都与ASP.NET有关)。此外,大多数答案与运行时错误有关,而不是与添加迁移时的错误有关 我正在尝试设置一个使用EntityFrameWork Core的.NET Core 3 WPF项目,但在添加迁移时遇到问题。我已经设置了我的上下文如下 public class ApplicationDbContext : DbContext { public ApplicationDb
public class ApplicationDbContext : DbContext {
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options) {
}
public ApplicationDbContext() {
}
public DbSet<Product> Products { get; set; }
}
我意识到其中一些是不相关的,但我想我应该向全班展示一下,以防它揭示出我遗漏的东西。代码基于此
但是,当我尝试添加迁移时,会出现一个异常“尚未为此DbContext配置任何数据库提供程序。可以通过重写DbContext.OnConfigurang方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。如果使用了AddDbContext,还应确保DbContext类型在其构造函数中接受DbContextOptions对象,并将其传递给DbContext的基本构造函数。”
就我所见,我已经配置了数据库提供程序。我在ConfigureServices
方法中放置了一个断点,可以看到services.AddDbContext
是用正确的连接字符串调用的
有人知道我错过了什么吗
更新我尝试连接到一个现有的数据库,它工作得非常好,因此看起来数据库提供程序的配置是正确的。只有当我尝试添加迁移时,我才会出现异常
更新2迁移工具似乎在上下文中使用无参数构造函数,这就是它认为未配置提供程序的原因。如果我从App.xaml.cs中删除配置它的行,并替代
onconfigurang
方法调用UseSqlServer
,则迁移工作将失败ks很好。但是,除了我没有看到其他人这样做之外(这让我怀疑这是否真的是正确的方式),我不知道如何从配置文件中获取连接字符串。我无法插入IConfiguration
参数,因为整个问题是迁移需要一个无参数构造函数。您需要实现IDesignTimeDbContextFactory
。在处理wir的ASP.NET核心应用程序中有很多隐藏的管道正在设置应用程序服务提供商,以便可以通过dotnet ef
工具找到它,但WPF中不存在此类管道。此外,ef工具对WPF事件一无所知,因此您的OnStartup
方法甚至不会被调用(甚至不会创建该类的实例)创建DI设置,以便ef工具可以找到您的DBContext
将创建ServiceProvider
的代码移到构造函数中,而不是查找主窗口并显示它的位
实现
IDesignTimeDbContextFactory使用.Net Core 3.1和EF Core Version 5实际上非常简单,Entity Framework将查看静态函数CreateHostBuilder
的入口点类,在我的例子中,它将是App.xaml.cs中的App
类
不完全确定.Net Core 3.1之前所需的约定。根据我的经验,它与.Net Core 2.1和ASP.Net的启动类有关
我的解决方案:
public partial class App : Application
{
/// <summary>
/// Necessary for EF core to be able to find and construct
/// the DB context.
/// </summary>
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
// Configure Application services
.ConfigureServices((context, services) =>
{
ConfigureServices(context, services);
});
}
/// <summary>
/// Not necessary but I prefer having a separate function for
/// configuring services.
/// </summary>
private static void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
...
}
/// <summary>
/// Hold the built Host for starting and stopping
/// </summary>
private readonly IHost AppHost;
/// <summary>
/// Constructor
/// </summary>
public App()
{
// Create Application host
AppHost = CreateHostBuilder(new string[] { }).Build();
}
/// <summary>
/// App Startup Event Handler
/// </summary>
private async void Application_Startup(object sender, StartupEventArgs e)
{
// Start the application host
await AppHost.StartAsync();
...
}
/// <summary>
/// App Exit Event Handler
/// </summary>
private async void Application_Exit(object sender, ExitEventArgs e)
{
// Kill the application host gracefully
await AppHost.StopAsync(TimeSpan.FromSeconds(5));
// Dispose of the host at the end of execution
AppHost.Dispose();
}
}
公共部分类应用程序:应用程序
{
///
///EF core必须能够找到并构建
///数据库上下文。
///
公共静态IHostBuilder CreateHostBuilder(字符串[]args)
{
返回Host.CreateDefaultBuilder(args)
//配置应用程序服务
.ConfigureServices((上下文、服务)=>
{
配置服务(上下文、服务);
});
}
///
///没有必要,但我更喜欢为
///配置服务。
///
专用静态void配置服务(HostBuilderContext上下文、iSeries收集服务)
{
...
}
///
///按住内置主机以启动和停止
///
专用只读IHost AppHost;
///
///建造师
///
公共应用程序()
{
//创建应用程序主机
AppHost=CreateHostBuilder(新字符串[]{}).Build();
}
///
///应用程序启动事件处理程序
///
私有异步无效应用程序\u启动(对象发送方、StartupEventArgs e)
{
//启动应用程序主机
等待AppHost.StartAsync();
...
}
///
///应用程序退出事件处理程序
///
私有异步无效应用程序\u出口(对象发送方,出口目标)
{
//优雅地杀死应用程序主机
等待AppHost.StopAsync(TimeSpan.FromSeconds(5));
//在执行结束时处置主机
AppHost.Dispose();
}
}
你解决了吗?我也想知道怎么做。@DreamingOfSleep不,我没有。真不敢相信我们是唯一尝试过这个的人!放弃。客户已经同意使用web应用程序而不是WPF,我厌倦了把时间浪费在一些似乎破损严重、文档记录不完整的东西上,这让我想知道任何人都在使用它。
public partial class App : Application
{
/// <summary>
/// Necessary for EF core to be able to find and construct
/// the DB context.
/// </summary>
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
// Configure Application services
.ConfigureServices((context, services) =>
{
ConfigureServices(context, services);
});
}
/// <summary>
/// Not necessary but I prefer having a separate function for
/// configuring services.
/// </summary>
private static void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
...
}
/// <summary>
/// Hold the built Host for starting and stopping
/// </summary>
private readonly IHost AppHost;
/// <summary>
/// Constructor
/// </summary>
public App()
{
// Create Application host
AppHost = CreateHostBuilder(new string[] { }).Build();
}
/// <summary>
/// App Startup Event Handler
/// </summary>
private async void Application_Startup(object sender, StartupEventArgs e)
{
// Start the application host
await AppHost.StartAsync();
...
}
/// <summary>
/// App Exit Event Handler
/// </summary>
private async void Application_Exit(object sender, ExitEventArgs e)
{
// Kill the application host gracefully
await AppHost.StopAsync(TimeSpan.FromSeconds(5));
// Dispose of the host at the end of execution
AppHost.Dispose();
}
}