将DbContext与依赖项注入一起使用

时间:2020-10-26 21:11:02

标签: c# wpf entity-framework mvvm dbcontext

我在MVVM体系结构中构建WPF应用程序。按下按钮应该给我来自DataGrid上数据库的数据。应用程序正确构建,可以启动它,但是当我按下按钮时,我得到“对象引用[...]”,有关dbContext的信息为空。

下面的代码:

AuctionDbContext.cs

 public class AuctionDbContext: DbContext
    {

        public AuctionDbContext(DbContextOptions<AuctionDbContext> options): base(options)
        {

            /* Database.EnsureCreated();*/
        }

        public DbSet<Auction> Auctions { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {            
            base.OnModelCreating(modelBuilder);
        }

App.cs

public partial class App : Application
    {
        private ServiceProvider serviceProvider;
        private DbCreator dbCreator = new DbCreator();


        public App()
        {
            ServiceCollection services = new ServiceCollection();

            services.AddDbContext<AuctionDbContext>(option =>
            {
                option.UseSqlite("Data Source = " + DbCreator.DATABASE_FILE_PATH);
            });

            services.AddSingleton<MainWindow>();

            serviceProvider = services.BuildServiceProvider();

        }

        private void OnStartup(object sender, StartupEventArgs e)
        {
            dbCreator.createDbFile();
            dbCreator.createConnectionToDatabase();
            dbCreator.createTable();
            dbCreator.fillTable();

            var mainWindow = serviceProvider.GetService<MainWindow>();
            mainWindow.Show();

        }
        
    }
}

MainWindow.cs

public partial class MainWindow : Window
{
    AuctionDbContext dbContext;
    public MainWindow()
    {
        InitializeComponent();
    }

    private void MarketMenu_Clicked(object sender, RoutedEventArgs e)
    {
        DataContext = new MarketViewModel(dbContext);
    }
}

MarketViewModel.cs

   public class MarketViewModel
    {
        AuctionDbContext dbContext;
        MarketView marketView = new MarketView();

        public MarketViewModel(AuctionDbContext dbContext)
        {
            this.dbContext = dbContext;
            GetAuctions();
        }

        private void GetAuctions()
        {
            marketView.AuctionDG.ItemsSource = dbContext.Auctions.ToList(); /* Here I got error */
        }
    }
}

dbContext was null

我使用了此文档,但没有看到任何错误:( https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext

之前,当我将所有内容都放在mainWindow类中时,一切正常,但这就是PoC。当我将项目重构为MVVM时出了点问题。我花了几个小时寻找解决方案,但没有成功。

如果有帮助,这是我在GitHub https://github.com/BElluu/EUTool上的仓库。查看分支:master的 1-recomm-mvvm 的Coz已过时了:)

1 个答案:

答案 0 :(得分:2)

您似乎没有初始化dbContext中的MainWindow字段:

public partial class MainWindow : Window
{
    AuctionDbContext dbContext;
    public MainWindow(AuctionDbContext dbContext)
    {
        this.dbContext = dbContext;
        InitializeComponent();
    }

    private void MarketMenu_Clicked(object sender, RoutedEventArgs e)
    {
        DataContext = new MarketViewModel(dbContext);
    }
}