[英]entity-framework throws error only while publish in single file
I have a .net core 3.1 WPF sample project with the entity-framework core&sqlite.我有一个带有实体框架 core&sqlite 的 .net core 3.1 WPF 示例项目。
Here is the code in Mainwindow.XAML:这是 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());
}
}
}
}
Here is the code of XAML:下面是 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>
Here is the code of database:下面是数据库的代码:
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);
}
}
}
}
Here is the code of database model:这是数据库模型的代码:
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: I placed the database.db in the same folder as my program file. PS:我将database.db 放在与我的程序文件相同的文件夹中。
If I debug the program or publish it without Produce Single File
, all works well.如果我调试程序或在没有Produce Single File
情况下发布它,一切正常。
However, after I published it in a single file, it runs with an error above:但是,在我将其发布到单个文件中后,它运行时出现上述错误:
What's wrong with it?它出什么问题了? And how can I solve this?我该如何解决这个问题? Thank you.谢谢你。
Problem solved.问题解决了。
System.AppDomain.CurrentDomain.BaseDirectory
doesn't get the right directory of the program. System.AppDomain.CurrentDomain.BaseDirectory
没有获得程序的正确目录。
It seems the Single File program will extract all the files into a temporary folder and run.似乎 Single File 程序会将所有文件提取到一个临时文件夹中并运行。
The System.AppDomain.CurrentDomain.BaseDirectory
is just getting the path of the temporary folder but not the program folder. System.AppDomain.CurrentDomain.BaseDirectory
只是获取临时文件夹的路径,而不是程序文件夹的路径。
As https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file said, I should use AppContext.BaseDirectory
instead of it.正如https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file所说,我应该使用AppContext.BaseDirectory
而不是它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.