0

我正在创建一个 C# WPF 应用程序,老实说,我并不是最好的。

我想知道是否可以执行以下操作。

当应用程序启动时,我希望它自动检查本地数据库中的表并且表为空,创建一个弹出窗口提示用户插入填充这些行所需的值?

这是可能的还是我将不得不考虑另一种设计?

目前我在我的 Window_loaded 中:

if (limit.UL == null && limit.LL == null)
{
    limitWindow = new LimitWindow();
}

其中 limit.UL 和 limit.LL 是我表中的列,但由于它们不是对象而没有任何运气。

有什么建议吗?

提前致谢。

4

1 回答 1

0

我建议您覆盖Application.OnStartupApp.xaml.cs 文件中的方法。在那里,我会检查您的数据是否存在。如果没有,我会创建您的LimitWindow并将其显示为对话框。之后,可以初始化并显示应用程序主窗口。代码看起来有点像这样:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    // Check if the database needs to be updated; if yes, show the corresponding window as a dialog
    var limit = CheckLimit();
    if (limit.UL == null && limit.LL == null)
    {
        var limitWindow = new LimitWindow();
        var dialogResult = limitWindow.ShowDialog();
        if (dialogResult)
        {
            // Update your database here
        }
    }

    // Show the actual main window of the application after the check
    this.MainWindow = new MainWindow();
    this.MainWindow.Show();
}

请记住,您应该从App.xaml 文件中StartupUri的元素中删除该属性。Application

CheckLimit函数将实例化实体框架DbContextObjectContext实体框架并执行查询,通常使用 LINQ:

private Limit CheckLimit()
{
    // Create the context and perform the query
    var dbContext = new ClassDerivedFromDbContext();
    var limit = dbContext.Limits.FirstOrDefault();
    dbContext.Close();
    return limit;
}

由于我不知道您的实体框架类的外观如何,您必须CheckLimit自己实现。

希望这可以帮助。

于 2013-04-26T15:57:38.453 回答