1

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 was null

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 :)

1 Answer 1

3

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Holy moly! Thank you so much! That's was that. I have another error now but I will fight with that :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.