最近我将我的 WPF 应用程序 (.NET 4.7) 迁移到了 .Net Core 3.1。除了我无法生成/添加 EF Core 迁移之外,大部分都可以正常工作。
dbContext
我使用依赖注入在以下帖子的帮助下集成了我的/ EF Core:
- https://marcominerva.wordpress.com/2019/03/06/using-net-core-3-0-dependency-injection-and-service-provider-with-wpf/
- https://marcominerva.wordpress.com/2019/03/19/using-entity-framework-core-with-wpf-on-net-core-3-0/
数据库连接并正常工作。但是,当我尝试通过键入以下命令来添加迁移时:Add-Migration InitialMigration
我不断收到以下错误:
无法创建“MyDBContext”类型的对象。有关设计时支持的不同模式,请参阅https://go.microsoft.com/fwlink/?linkid=851728
在 Web 应用程序中,这似乎是一个常见/已知问题,可以通过在程序类中更改CreateHostBuilder
为来解决;BuildWebHost
https://stackoverflow.com/questions/57745481/unable-to-create-an-object-of-type-mycontext-for-the-different-patterns-suppo
MigrationsAssembly
我还通过添加找到了其他一些帖子options.UseSqlServer
。
长话短说,我尝试了所有这些解决方案,但似乎都没有奏效。可能是因为我的应用程序不是 Web 应用程序(没有程序类,例如 BuildWebHost),而是 WPF(核心)应用程序。
我的 WPF 应用程序的初始化代码如下所示:
应用程序.xaml.cs
public partial class App : Application
{
private readonly IHost host;
public IServiceProvider ServiceProvider { get; private set; }
public IConfiguration Configuration { get; private set; }
public App()
{
host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
ConfigureServices(services);
}).Build();
}
private void ConfigureServices(IServiceCollection services)
{
// services for DI...
services.AddDbContext<MyDbContext>
(options =>
options.UseSqlServer(
Configuration.GetConnectionString("SqlConnection")));
services.AddTransient<MainWindow>();
}
protected override void OnStartup(StartupEventArgs e)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
Configuration = builder.Build();
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
ServiceProvider = serviceCollection.BuildServiceProvider();
var mainWindow = ServiceProvider.GetRequiredService<MainWindow>();
mainWindow.Show();
}
protected override async void OnExit(ExitEventArgs e)
{
using (host)
{
await host.StopAsync(TimeSpan.FromSeconds(5));
}
base.OnExit(e);
}
}
MyDbContext.cs
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
Database.Migrate();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Zonnescherm Model
modelBuilder.Entity<Zonneschermen>().Property(e => e.BreedteZonnescherm).HasColumnType("decimal(17,2)");
modelBuilder.Entity<Zonneschermen>().Property(e => e.UitvalDoek).HasColumnType("decimal(17,2)");
modelBuilder.Entity<Zonneschermen>().Property(e => e.ArmLengte).HasColumnType("decimal(17,2)");
// ScreenRegels Model
modelBuilder.Entity<ScreenRegels>().Property(e => e.Breedte).HasColumnType("decimal(17,2)");
modelBuilder.Entity<ScreenRegels>().Property(e => e.Hoogte).HasColumnType("decimal(17,2)");
modelBuilder.Entity<ScreenRegels>().Property(e => e.DraaistangLengte).HasColumnType("decimal(17,2)");
// Seed
modelBuilder.Entity<Instellingen>().HasData(
// seeding...
);
// Seed
modelBuilder.Entity<ZonneschermInstellingen>().HasData(
// seeding...
);
// Seed
modelBuilder.Entity<ScreenInstellingen>().HasData(
// seeding...
);
}
public DbSet<Lamellen> Lamellen { get; set; }
public DbSet<LamellenRegels> LamellenRegels { get; set; }
public DbSet<Zonneschermen> Zonneschermen { get; set; }
public DbSet<ZonneschermInstellingen> ZonneschermInstellingen { get; set; }
public DbSet<Screens> Screens { get; set; }
public DbSet<ScreenRegels> ScreenRegels { get; set; }
public DbSet<ScreenInstellingen> ScreenInstellingen { get; set; }
public DbSet<Instellingen> Instellingen { get; set; }
}
有什么线索哪里出错了吗?