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

发布于 2024-12-21 14:47:31 字数 1428 浏览 0 评论 0原文

中创建迁移 .cs 文件

  • 默认情况下,add-migration 命令尝试在项目根目录
    • 迁移

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

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

使用这种结构,当我在 NuGet 控制台中执行时,

PM> add-migration Migration1

我收到以下错误:

    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 命令时应创建迁移文件的磁盘位置?< /强>

By default, the add-migration command attempts to create the migration .cs file in

  • Project Root
    • Migrations

I'd like to store my migrations along with the rest of my EF-related code in the \Data folder of my project:

  • Project Root
    • Data
      • Migrations

With this structure, when I execute

PM> add-migration Migration1

in the NuGet console I receive the following error:

    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)

Is it possible to specify the location on disk that the migration file should be created when executing the add-migration command?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三寸金莲 2024-12-28 14:47:31

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

this.MigrationsDirectory = "DirOne\\DirTwo";

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

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

In the configuration class constructor add this line:

this.MigrationsDirectory = "DirOne\\DirTwo";

The namespace will continue to be set as the namespace of the configuration class itself. To change this add this line (also in the configuration constructor):

this.MigrationsNamespace = "MyApp.DirOne.DirTwo";
倾城花音 2024-12-28 14:47:31

也可以在调用 enable-migrations 命令(创建 Configuration 类)期间使用 -MigrationsDirectory 参数指定迁移文件夹:

enable-migrations -EnableAutomaticMigration:$false -MigrationsDirectory Migrations\CustomerDatabases -ContextTypeName FullyQualifiedContextName

该示例将创建一个 Configuration 类,它将 MigrationsDirectory 设置为相对于项目根文件夹的指定文件夹“Migrations\CustomerDatabases”。

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

另请参阅这篇文章,其中介绍了具有多个上下文和迁移文件夹的项目。

顺便说一句,如果您使用多个迁移文件夹和多个上下文,还请考虑在 DbContext 派生类的 OnModelCreating 方法中设置默认架构的名称(Fluent-API 配置所在的位置)。
这将在 EF6 中起作用:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("CustomerDatabases");
    }

它将使用架构名称为数据库表添加前缀。这将使您能够在具有多组相互独立的表的情况下将多个上下文与单个数据库一起使用。
(这还将创建单独版本的 MigrationHistory 表,在上面的示例中为 CustomerDatabases.__MigrationHistory)。

Specifying the migrations folder is also possible during the invoke of the enable-migrations command (which creates the Configuration class), using the -MigrationsDirectory parameter:

enable-migrations -EnableAutomaticMigration:$false -MigrationsDirectory Migrations\CustomerDatabases -ContextTypeName FullyQualifiedContextName

The example will create a Configuration class which sets the MigrationsDirectory to the specified folder 'Migrations\CustomerDatabases' which is relative to the projects root folder.

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

See also this article which explains about a project with multiple contexts and migration folders.

By the way, if you are using multiple migrations folders and multiple contexts, please consider also to set up a name for the default schema in the OnModelCreating method of you DbContext derived class (where the Fluent-API configuration is).
This will work in EF6:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("CustomerDatabases");
    }

The will prefix you database tables with the schema name. This will enable you to use more than one context with a single database in a scenario where you have several groups of tables which are independent from another.
(This will also create separate versions of the MigrationHistory tables, in the example above it would be CustomerDatabases.__MigrationHistory).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文