c# - 实体框架仅在单个文件中发布时引发错误
问题描述
我有一个带有实体框架核心&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
,一切正常。
它出什么问题了?我该如何解决这个问题?谢谢你。
解决方案
问题解决了。
System.AppDomain.CurrentDomain.BaseDirectory
没有得到程序的正确目录。
似乎 Single File 程序会将所有文件提取到一个临时文件夹中并运行。
System.AppDomain.CurrentDomain.BaseDirectory
只是获取临时文件夹的路径,而不是程序文件夹。
正如https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file所说,我应该使用AppContext.BaseDirectory
它来代替它。
推荐阅读
- gulp - 使用 Gulp,是否可以将环境变量作为注释添加到 CSS 中?(例如 package.json 版本)
- java - 定义嵌套自定义注解时“嵌套类型寄存器无法隐藏封闭类型”
- php - Wordpress $query->set() 出错致命错误:调用字符串上的成员函数 set()
- python-3.x - Hangman Python 3 关于匹配
- apache-spark - 使用日期列 Java-Spark 写入分区
- javascript - 未来无法正常工作
- python - 用 Python 解释决策树
- rest - 使用 firebase 进行 Spring Boot 和 vueJs 身份验证
- javascript - 未捕获(承诺)错误错误请求反应
- meta-tags - 即使页面上没有使用文章的图像,og:image 元标记和 JSON-ld 也能工作吗?