首页 > 解决方案 > 实体框架仅在单个文件中发布时引发错误

问题描述

我有一个带有实体框架核心&sqlite 的.net core 3.1 WPF 示例项目。

这是 Mainwindow.XAML 中的代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }



        public ObservableCollection<string> ObjectList { get; set; } 

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                Database.AppDBContext Context = new Database.AppDBContext();
                ObjectList = new ObservableCollection<string>(Context.TestTable.Select(X => X.name));
                this.DataContext = this;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}

这是 XAML 的代码:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
    <Grid>
        <ListBox ItemsSource="{Binding ObjectList}">
            
        </ListBox>
    </Grid>
</Window>

下面是数据库的代码:

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace WpfApp1.Database
{
    public class AppDBContext : DbContext
    {
        public virtual DbSet<TestModel> TestTable { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string Path = System.AppDomain.CurrentDomain.BaseDirectory + @"\database.db";
            if (File.Exists(Path))
            {
                optionsBuilder.UseSqlite("Data Source=" + Path);
            }
        }
    }
}

这是数据库模型的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace WpfApp1.Database
{
    public class TestModel
    {
        [Key]
        public string id { get; set; }
        public string name { get; set; }
    }
}

PS:我将database.db 与我的程序文件放在同一个文件夹中。

如果我调试程序或在没有 的情况下发布它Produce Single File,一切正常。

但是,在我将其发布到单个文件中后,它运行时出现上述错误: 在此处输入图像描述


在此处输入图像描述

它出什么问题了?我该如何解决这个问题?谢谢你。

标签: c#wpfentity-frameworkentity-framework-core

解决方案


问题解决了。

System.AppDomain.CurrentDomain.BaseDirectory没有得到程序的正确目录。

似乎 Single File 程序会将所有文件提取到一个临时文件夹中并运行。

System.AppDomain.CurrentDomain.BaseDirectory只是获取临时文件夹的路径,而不是程序文件夹。

正如https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file所说,我应该使用AppContext.BaseDirectory它来代替它。


推荐阅读