Entity framework 4 是否可以更改EF迁移的位置;迁移“;文件夹?

Entity framework 4 是否可以更改EF迁移的位置;迁移“;文件夹?,entity-framework-4,entity-framework-migrations,Entity Framework 4,Entity Framework Migrations,默认情况下,add migration命令尝试在中创建migration.cs文件 项目根 迁移 我想将我的迁移和其他与EF相关的代码存储在项目的\Data文件夹中: 项目根 资料 迁移 在这个结构中,当我执行 PM> add-migration Migration1 在NuGet控制台中,我收到以下错误: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\MyPro

默认情况下,add migration命令尝试在中创建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) System.IO.DirectoryNotFoundException:找不到路径“C:\MyProjectRoot\Migrations\201112171635110\u Migration1.cs”的一部分。 在System.IO.\uuu Error.WinIOError(Int32 errorCode,字符串maybeFullPath) 在System.IO.FileStream.Init(字符串路径、文件模式、文件访问权限、Int32权限、布尔用户权限、文件共享、Int32缓冲大小、文件选项选项、安全属性secAttrs、字符串msgPath、布尔bFromProxy、布尔useLongPath) 位于System.IO.FileStream..ctor(字符串路径、文件模式、文件访问访问、文件共享、Int32 bufferSize、文件选项) 位于System.IO.StreamWriter.CreateFile(字符串路径,布尔追加) 在System.IO.StreamWriter..ctor(字符串路径、布尔追加、编码、Int32 bufferSize) 在System.IO.StreamWriter..ctor(字符串路径、布尔追加、编码) 位于System.IO.File.InternalWriteAllText(字符串路径、字符串内容、编码) 在System.IO.File.WriteAllText(字符串路径,字符串内容)
执行添加迁移命令时,是否可以在磁盘上指定迁移文件应创建的位置?在配置类构造函数中添加此行:

this.MigrationsDirectory = "DirOne\\DirTwo";
名称空间将继续设置为配置类本身的名称空间。要更改此项,请添加此行(也在配置构造函数中):


在调用
启用迁移
命令(创建
配置
类)期间,也可以使用
-migrationdirectory
参数指定迁移文件夹:

enable-migrations -EnableAutomaticMigration:$false -MigrationsDirectory Migrations\CustomerDatabases -ContextTypeName FullyQualifiedContextName
该示例将创建一个
Configuration
类,该类将
MigrationsDirectory
设置为相对于项目根文件夹的指定文件夹“Migrations\customerdatabase”

public Configuration()
{
    AutomaticMigrationsEnabled = false;
    MigrationsDirectory = @"Migrations\CustomerDatabases";
}

另请参阅一篇文章,该文章解释了具有多个上下文和迁移文件夹的项目

顺便说一下,如果您使用多个迁移文件夹和多个上下文,请考虑在<代码> OnMeultDebug < /Case>方法中设置默认模式的名称:<代码> DbValue派生类(其中FLUENT API配置是)。 这将在EF6中起作用:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("CustomerDatabases");
    }
将使用架构名称作为数据库表的前缀。这将使您能够在一个场景中对单个数据库使用多个上下文,其中您有多组独立于另一组的表。
(这还将创建迁移历史记录表的单独版本,在上面的示例中,它将是
客户数据库。u MigrationHistory
)。

请将Roger的解决方案标记为答案。这对我有用。谢谢。+1帮我省去了很多麻烦。我浏览了所有的Pluralsight视频,试图找到一个不继承迁移文件夹位置的默认配置的示例。你是一个救命恩人。有没有办法在
web.config
文件中指定这个?这个配置类在哪里?路径是相对于项目根目录的,因此
@“Migrations\Context1”
将是一个示例路径。如果在第一次成功创建该文件后,立即发现该文件存在一个奇怪的错误,则可能是错误地放置了
@“Migrations\\Context1”
。()
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("CustomerDatabases");
    }