简体   繁体   中英

Using DbContext with dependency injection

I building WPF application in MVVM architecture. Pressing button should give me data from database on DataGrid. App correctly build and I can start it but when I press button I get "Object reference[...]" and information about dbContext was null.

Below some code:

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 为空

I used this doc and I do not see any mistake:( 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. Something went wrong, when I refactor project to 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 . Look at branch: 1-refactor-to-mvvm coz of master is outdated yet:)

You don't seem to initialize the dbContext field in the 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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