【发布时间】: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 的第一个应用程序,所以我不知道问题出在哪里。
谢谢。
【问题讨论】:
-
No parameterless constructor defined for type 'ProdTool.Models.AppDataContext'. -
请显示 appsettings.json
标签: c# wpf entity-framework