reg*_*ahn 5 .net c# entity-framework ef-migrations
我在项目中使用Entity Framework代码优先迁移。这些迁移之一通过从csv位于项目目录中的文件中读取数据来填充数据库表。项目结构如下所示:
- Soulution
- Project
- Migrations
- CSVFolder
- file1.csv
- file2.csv
- Migration1.cs
- Migration2.cs
Run Code Online (Sandbox Code Playgroud)
用我的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.
}
}
Run Code Online (Sandbox Code Playgroud)
这在我的开发机上运行良好,但是当我使用Octopus将其部署到测试服务器时,出现未找到路径的异常
找不到路径“ C:\ Octopus \ Applications \ Test \ Solution \ 1.1-alpha21 \ Migrations \ CSVFolder”的一部分。
我已经检查了'drop'文件夹,其中的输出包括Migrations > CSVFolder。
我尝试了几种其他方法来获取文件夹的路径,但是它们可以在本地或在服务器上工作。获得在本地和服务器上均可使用的路径的最佳方法是什么?
路径一定要设置正确!AppDomain.CurrentDomain.BaseDirectory在测试测试运行程序目录的情况下将返回,在应用程序模式下,您将得到 ..\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.
Run Code Online (Sandbox Code Playgroud)
你已经注意到你正在尝试做的事情并不容易,所以你可以忘记这个方法,这就是技巧。最好的方法是将 cvs 文件附加到程序集资源!在迁移过程中,您可以按如下方式访问它们:
在向上()
var file1 = Properties.Resources.File1;
Run Code Online (Sandbox Code Playgroud)
这种方法将保证 Sql 或 Cvs 文件始终在运行时附加/加载到程序集。
| 归档时间: |
|
| 查看次数: |
675 次 |
| 最近记录: |