gpt4 book ai didi

c# - Entity Framework Core 与 WPF Core - 无法添加迁移

转载 作者:行者123 更新时间:2023-12-04 04:17:45 24 4
gpt4 key购买 nike

最近我将我的 WPF 应用程序 (.NET 4.7) 迁移到了 .Net Core 3.1。除了我无法生成/添加 EF Core 迁移外,大部分工作正常。

我在以下帖子的帮助下使用依赖注入(inject)集成了我的 dbContext/EF Core:

数据库连接正常。但是,当我尝试通过键入以下命令来添加迁移时:Add-Migration InitialMigration 我不断收到以下错误:

Unable to create an object of type 'MyDBContext'. For the different patterns supported at design time, see 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

我还通过在 options.UseSqlServer 中添加 MigrationsAssembly 找到了一些其他帖子。

长话短说,我尝试了所有这些解决方案,但似乎都不起作用。可能是因为我的应用程序不是 Web 应用程序(没有程序类,例如 BuildWebHost),而是 WPF(核心)应用程序。

我的 WPF 应用程序初始化代码如下所示:

App.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; }
}

有什么地方出错的线索吗?

最佳答案

对于 .net core 5(不确定它是否适用于早期版本)

只需将此类添加到您的项目中

public class BloggingContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseSqlServer("your connection string",
options => options.EnableRetryOnFailure());

return new ApplicationDbContext(optionsBuilder.Options);
}
}

文档链接 https://learn.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli#from-a-design-time-factory

关于c# - Entity Framework Core 与 WPF Core - 无法添加迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60226267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com