是否可以更改EF Migrations“Migrations”文件夹的位置?

时间:2011-12-17 16:44:43

标签: entity-framework-4 ef-migrations

默认情况下,add-migration命令会尝试在

中创建迁移.cs文件
  • 项目根目录
    • 迁移

我想将我的迁移以及其余的EF相关代码存储在我项目的\ Data文件夹中:

  • 项目根目录
    • 数据
      • 迁移

使用这种结构,当我执行

PM> add-migration Migration1

在NuGet控制台中,我收到以下错误:

    System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\MyProjectRoot\Migrations\201112171635110_Migration1.cs'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.StreamWriter.CreateFile(String path, Boolean append)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
   at System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding)
   at System.IO.File.WriteAllText(String path, String contents)

是否可以在执行add-migration命令时指定磁盘上应创建迁移文件的位置?

1 个答案:

答案 0 :(得分:62)

在配置类构造函数中添加以下行:

this.MigrationsDirectory = "DirOne\\DirTwo";

命名空间将继续设置为配置类本身的命名空间。要更改此行,请添加此行(也在配置构造函数中):

this.MigrationsNamespace = "MyApp.DirOne.DirTwo";