简体   繁体   English

从EF迁移脚本中查找目录路径的最佳方法是什么?

[英]What is the best way to find directory path from an EF migration script?

I'm using Entity Framework code first migrations in my project. 我正在我的项目中使用Entity Framework代码进行首次迁移。 One of these migrations populates a database table by reading in data from csv files located in a directory in the project. 其中一个迁移通过从位于项目目录中的csv文件中读取数据来填充数据库表。 The project structure looks like this: 项目结构如下所示:

- Soulution
    - Project
      - Migrations
        - CSVFolder
          - file1.csv
          - file2.csv
      - Migration1.cs
      - Migration2.cs

In my Up() method, I get these files like so: 在我的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.
    }
}

This runs fine on my dev machine but when I deploy this to the test server with Octopus, I get a path not found exception 这在我的开发机器上运行正常但是当我使用Octopus将其部署到测试服务器时,我得到一个路径未找到异常

Could not find a part of the path 'C:\\Octopus\\Applications\\Test\\Solution\\1.1-alpha21\\Migrations\\CSVFolder'. 找不到路径'C:\\ Octopus \\ Applications \\ Test \\ Solution \\ 1.1-alpha21 \\ Migrations \\ CSVFolder'的一部分。

I've checked the 'drop' folder and the output there includes Migrations > CSVFolder . 我检查了'drop'文件夹,其输出包括Migrations > CSVFolder

I've tried a few different ways to get the path to the folder but they work either locally or on the server. 我尝试了几种不同的方法来获取文件夹的路径,但它们可以在本地或服务器上运行。 What's the best way to get the path that will work both locally and on the server? 获取本地和服务器上的路径的最佳方法是什么?

You have to set the path correctly! 你必须正确设置路径! AppDomain.CurrentDomain.BaseDirectory will returns in case of testing the test runner directory and in the application mode you will get ..\\Bin\\Debug etc. You have to check how many directories you have until you can reach the desired location(CSVFolder) and then replace the path or change it for example : 如果测试测试运行器目录, 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.

You have noticed that is not easy what you are trying to do so you can forget this apporach and this is the trick. 你已经注意到你要做的事情并不容易,所以你可以忘记这个应用,这就是诀窍。 The best way is just attach the cvs files to the assembly resources! 最好的方法是将cvs文件附加到程序集资源! and during the migrations you can access them like the following: 在迁移过程中,您可以像下面这样访问它们:

in Up() 在Up()

var file1 = Properties.Resources.File1;

This approach will guarantee that the Sql or Cvs files will always attached/loaded to the assembly at the runtime. 此方法将保证Sql或Cvs文件将始终在运行时附加/加载到程序集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM