【发布时间】:2021-03-20 05:22:06
【问题描述】:
我的 SqlLite 数据库迁移引发错误:
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'ProductDbContext'.
---> System.MissingMethodException: No parameterless constructor defined for type 'WpfApp1.Data.ProductDbContext'.
这是我的 DbContext
public class ProductDbContext : DbContext
{
#region Constructor
public ProductDbContext(DbContextOptions<ProductDbContext> options) : base(options)
{
Database.EnsureCreated();
}
#endregion
#region Public properties
public DbSet<Product> Products { get; set; }
public DbSet<Category> Types { get; set; }
#endregion
#region Overridden methods
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new ProductDbConfig());
modelBuilder.ApplyConfiguration(new CategoryDbConfig());
modelBuilder.Entity<Product>().HasData(GetProducts());
base.OnModelCreating(modelBuilder);
}
private Product[] GetProducts()
{
// create seed data
}
}
我有一个 App.xaml.cs 我在其中进行配置
public partial class App : Application
{
#region Private members
private readonly ServiceProvider serviceProvider;
#endregion
#region Constructor
public App()
{
ServiceCollection services = new ServiceCollection();
services.AddDbContext<ProductDbContext>(options =>
{
options.UseSqlite("Data Source = Product.db");
});
services.AddSingleton<MainWindow>();
serviceProvider = services.BuildServiceProvider();
}
#endregion
#region Event Handlers
private void OnStartup(object s, StartupEventArgs e)
{
var mainWindow = serviceProvider.GetService<MainWindow>();
mainWindow.Show();
}
#endregion
}
我尝试在我的 dbContext 中添加一个无参数构造函数。没用。 是什么导致了这个错误?以及如何解决这个问题? 任何帮助表示赞赏!
【问题讨论】:
标签: c# wpf entity-framework sqlite asp.net-core