简体   繁体   English

将 DbContext 与依赖注入一起使用

[英]Using DbContext with dependency injection

I building WPF application in MVVM architecture.我在 MVVM 架构中构建 WPF 应用程序。 Pressing button should give me data from database on DataGrid.按下按钮应该给我 DataGrid 数据库中的数据。 App correctly build and I can start it but when I press button I get "Object reference[...]" and information about dbContext was null.应用程序正确构建并且我可以启动它但是当我按下按钮时我得到“对象引用[...]”并且关于 dbContext 的信息是 null。

Below some code:下面是一些代码:

AuctionDbContext.cs 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主窗口.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 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 为空

I used this doc and I do not see any mistake:( https://learn.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext我使用了这个文档,我没有看到任何错误:( https://learn.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext

Before, when I had all in mainWindow class, everything was ok but that was PoC.之前,当我在 mainWindow class 中拥有所有内容时,一切正常,但那是 PoC。 Something went wrong, when I refactor project to MVVM.当我将项目重构为 MVVM 时出了点问题。 I spent a couple of hours looking for a solution but without success.我花了几个小时寻找解决方案但没有成功。

If it will help, here's my repo on GitHub https://github.com/BElluu/EUTool .如果有帮助,这是我在 GitHub https://github.com/BElluu/EUTool上的回购协议。 Look at branch: 1-refactor-to-mvvm coz of master is outdated yet:)看看分支:master的 1-refactor-to-mvvm coz 已经过时了:)

You don't seem to initialize the dbContext field in the MainWindow :您似乎没有在MainWindow中初始化dbContext字段:

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM