从 EF 迁移脚本中查找目录路径的最佳方法是什么?
What is the best way to find directory path from an EF migration script?
我在我的项目中使用 Entity Framework 代码优先迁移。这些迁移之一通过从位于项目目录中的 csv
文件中读取数据来填充数据库 table。项目结构如下所示:
- Soulution
- Project
- Migrations
- CSVFolder
- file1.csv
- file2.csv
- Migration1.cs
- Migration2.cs
在我的 Up()
方法中,我得到这些文件是这样的:
public override void Up()
{
var migrationsDir = AppDomain.CurrentDomain.BaseDirectory + "/Migrations/CSVFolder/"; // For some reason, Path.Combine() doesn't work for me here so I manually concatenate the strings.
var templateFiles = Directory.GetFiles(migrationsDir, "*.csv");
foreach (var filename in templateFiles)
{
Sql(); // SQL Insert statement here.
}
}
这在我的开发机器上运行良好,但是当我使用 Octopus 将其部署到测试服务器时,出现路径未找到异常
Could not find a part of the path
'C:\Octopus\Applications\Test\Solution.1-alpha21\Migrations\CSVFolder'.
我检查了 'drop' 文件夹,那里的输出包括 Migrations > CSVFolder
。
我尝试了几种不同的方法来获取文件夹的路径,但它们要么在本地工作,要么在服务器上工作。获得既适用于本地又适用于服务器的路径的最佳方式是什么?
您必须正确设置路径! AppDomain.CurrentDomain.BaseDirectory
将 returns 在测试测试运行器目录的情况下,在应用程序模式下你将得到 ..\Bin\Debug 等。你必须检查你有多少目录,直到你可以到达所需的位置(CSVFolder),然后替换路径或更改它,例如:
var migrationsDir = AppDomain.CurrentDomain.BaseDirectory + "../../Migrations/CSVFolder/"; // For some reason, Path.Combine() doesn't work for me here so I manually concatenate the strings.
您已经注意到您尝试做的事情并不容易,所以您可以忘记这个方法,这就是诀窍。最好的方法就是将 cvs 文件附加到程序集资源!在迁移过程中,您可以像下面这样访问它们:
在上()
var file1 = Properties.Resources.File1;
此方法将保证 Sql 或 Cvs 文件在运行时始终 attached/loaded 到程序集。
这对我有用
AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory
我在我的项目中使用 Entity Framework 代码优先迁移。这些迁移之一通过从位于项目目录中的 csv
文件中读取数据来填充数据库 table。项目结构如下所示:
- Soulution
- Project
- Migrations
- CSVFolder
- file1.csv
- file2.csv
- Migration1.cs
- Migration2.cs
在我的 Up()
方法中,我得到这些文件是这样的:
public override void Up()
{
var migrationsDir = AppDomain.CurrentDomain.BaseDirectory + "/Migrations/CSVFolder/"; // For some reason, Path.Combine() doesn't work for me here so I manually concatenate the strings.
var templateFiles = Directory.GetFiles(migrationsDir, "*.csv");
foreach (var filename in templateFiles)
{
Sql(); // SQL Insert statement here.
}
}
这在我的开发机器上运行良好,但是当我使用 Octopus 将其部署到测试服务器时,出现路径未找到异常
Could not find a part of the path 'C:\Octopus\Applications\Test\Solution.1-alpha21\Migrations\CSVFolder'.
我检查了 'drop' 文件夹,那里的输出包括 Migrations > CSVFolder
。
我尝试了几种不同的方法来获取文件夹的路径,但它们要么在本地工作,要么在服务器上工作。获得既适用于本地又适用于服务器的路径的最佳方式是什么?
您必须正确设置路径! AppDomain.CurrentDomain.BaseDirectory
将 returns 在测试测试运行器目录的情况下,在应用程序模式下你将得到 ..\Bin\Debug 等。你必须检查你有多少目录,直到你可以到达所需的位置(CSVFolder),然后替换路径或更改它,例如:
var migrationsDir = AppDomain.CurrentDomain.BaseDirectory + "../../Migrations/CSVFolder/"; // For some reason, Path.Combine() doesn't work for me here so I manually concatenate the strings.
您已经注意到您尝试做的事情并不容易,所以您可以忘记这个方法,这就是诀窍。最好的方法就是将 cvs 文件附加到程序集资源!在迁移过程中,您可以像下面这样访问它们:
在上()
var file1 = Properties.Resources.File1;
此方法将保证 Sql 或 Cvs 文件在运行时始终 attached/loaded 到程序集。
这对我有用
AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory