【问题标题】:Unable to create an object of type 'DbContext' for Net Core 3.1 WPF app无法为 Net Core 3.1 WPF 应用程序创建类型为“DbContext”的对象
【发布时间】: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


    【解决方案1】:

    在我的 Xamarin.Forms 应用程序中,我在 DbContext 类中以这种方式使用它

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // Specify that we will use sqlite and the path of the database here
            var options = optionsBuilder
                .UseSqlite($"Data Source={SQLiteDatabasePath}");
    
            base.OnConfiguring(options);
        }
    

    == 编辑 ==

    我的基类中所有服务的单例构造:

            private static ETabberDb _DbContext;
    
            protected ETabberDb ETabberDbContext
            {
                get
                {
                    if (_DbContext == null)
                    {
                        var dbPath = DeviceService.GetSQLiteDatabasePath();
    
                        if (!File.Exists(dbPath))
                        {
                            File.Create(dbPath).Dispose();
                            new SQLite.SQLiteConnection(dbPath).Dispose();
                        }
    
                        ETabberDb.SQLiteDatabasePath = dbPath;
    
                        _DbContext = new ETabberDb();
                    }
    
                    return _DbContext;
                }
            }
    

    【讨论】:

    • 这是否意味着删除服务。我的 App.xaml.cs 的 App() 方法中的“AddDbContext...”代码?
    • 是的,我认为您应该删除该代码。
    • 但是您需要对 DbContext 进行某种实例化。我在服务的基类中使用单例。
    • 三思而后行。很可能是我的代码有缺陷。毕竟我不需要第一个声明。让我试试。一分钟。
    • 代码正确。你也需要 OnConfigure
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2020-10-28
    • 1970-01-01
    • 2020-11-16
    • 2021-11-10
    相关资源
    最近更新 更多