【问题标题】:Unable to create an object of type 'AppDataContext'无法创建“AppDataContext”类型的对象
【发布时间】:2020-11-09 20:51:15
【问题描述】:

我在 WPF 应用程序中设置迁移时遇到了一些麻烦。我有这样的DbContext 类:

    public class AppDataContext : DbContext
{
    public AppDataContext(DbContextOptions<AppDataContext> options) : base(options) { }

    public DbSet<Capacity> Capacities { get; set; }
    public DbSet<CapacityType> CapacityTypes { get; set; }
}

然后在 App.xaml.cs 中设置 DI 和 IoC:

    public partial class App : Application
{
    public IServiceProvider ServiceProvider { get; private set; }
    public IConfiguration Configuration { get; private set; }

    private void OnStartUp(object sender, StartupEventArgs e)
    {
        var ConfigBuilder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

        Configuration = ConfigBuilder.Build();

        ServiceCollection Services = new ServiceCollection();
        ConfigureServices(Services);
        ServiceProvider = Services.BuildServiceProvider();

        MainWindow MainWindow = ServiceProvider.GetService<MainWindow>();
        MainWindow.Show();
    }

    private void ConfigureServices(IServiceCollection Services)
    {
        Services.AddDbContext<AppDataContext>(options =>
        {
            options.UseSqlServer(Configuration.GetConnectionString("ProdToolDb"));
        });

        Services.AddSingleton<MainWindow>();
    }

}

如果我运行命令 Add-Migration Initial -verbose,我会收到以下消息:

Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'AppDataContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'AppDataContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
 ---> System.MissingMethodException: No parameterless constructor defined for type 'ProdTool.Models.AppDataContext'.
   at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_3.<FindContextTypes>b__13()
   --- End of inner exception stack trace ---
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_3.<FindContextTypes>b__13()
   at 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.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to create an object of type 'AppDataContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

由于这是我在 WPF 中使用 EF Core 3.1 的第一个应用程序,所以我不知道问题出在哪里。

谢谢。

【问题讨论】:

标签: c# wpf entity-framework


【解决方案1】:

你需要实现一个Design Time DbContext

public class AppDataContextFactory : IDesignTimeDbContextFactory<AppDataContext>
{
    public AppDataContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<AppDataContext>();
        var config = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .Build();

        optionsBuilder.UseSqlServer(config.GetConnectionString("ProdToolDb"));

        return new AppDataContext(optionsBuilder.Options);
    }
}

另外,我在App.xaml.cs 代码中看到了一个错误。您应该重写OnStartup(StartupEventArgs) 方法,正确的签名应该是:

protected override void OnStartup(StartupEventArgs e)
{
    var ConfigBuilder = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
    ...
}

重要提示:App.xaml 文件中删除StartupUri

【讨论】:

  • 它现在可以工作了,迁移已创建。但我不明白一件事。为什么我需要 Design Time DbContext,而在我的其他 ASP.net Core 应用程序中,我只是定义了我的 AppDataContext 并在 StartUp 类中添加了注册服务,一切正常。
  • 一些 EF Core 工具命令(例如迁移命令)需要在设计模式下创建的 DbContext。如果您的启动项目使用 ASP.NET Core Web Host 或 .NET Core Generic Host,这些工具会尝试从应用程序的服务提供程序获取 DbContext 对象。请阅读我附上的链接,那里有重要信息。
猜你喜欢
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-06
  • 2021-08-15
  • 2021-03-07
相关资源
最近更新 更多