【问题标题】:Using DbContext with dependency injection将 DbContext 与依赖注入一起使用
【发布时间】:2021-02-09 04:13:45
【问题描述】:

我在 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 */
        }
    }
}

我使用了这个文档,我没有发现任何错误:(https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext

以前,当我在 mainWindow 类中完成所有操作时,一切都很好,但那是 PoC。当我将项目重构为 MVVM 时,出了点问题。我花了几个小时寻找解决方案,但没有成功。

如果有帮助,这里是我在 GitHub 上的 repo https://github.com/BElluu/EUTool。查看分支: 1-refactor-to-mvvm 因为 master 的已经过时了 :)

【问题讨论】:

    标签: c# wpf entity-framework mvvm dbcontext


    【解决方案1】:

    您似乎没有初始化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);
        }
    }
    

    【讨论】:

    • 天哪!太感谢了!就是这样。我现在有另一个错误,但我会解决这个问题:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多