是否可以更改 EF 迁移“迁移”的位置?文件夹?
中创建迁移 .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
- Data
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在配置类构造函数中添加以下行:
命名空间将继续设置为配置类本身的命名空间。要更改此设置,请添加此行(也在配置构造函数中):
In the configuration class constructor add this line:
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):
也可以在调用
enable-migrations
命令(创建Configuration
类)期间使用-MigrationsDirectory
参数指定迁移文件夹:该示例将创建一个
Configuration
类,它将MigrationsDirectory
设置为相对于项目根文件夹的指定文件夹“Migrations\CustomerDatabases”。另请参阅这篇文章,其中介绍了具有多个上下文和迁移文件夹的项目。
顺便说一句,如果您使用多个迁移文件夹和多个上下文,还请考虑在
DbContext
派生类的OnModelCreating
方法中设置默认架构的名称(Fluent-API 配置所在的位置)。这将在 EF6 中起作用:
它将使用架构名称为数据库表添加前缀。这将使您能够在具有多组相互独立的表的情况下将多个上下文与单个数据库一起使用。
(这还将创建单独版本的 MigrationHistory 表,在上面的示例中为
CustomerDatabases.__MigrationHistory
)。Specifying the migrations folder is also possible during the invoke of the
enable-migrations
command (which creates theConfiguration
class), using the-MigrationsDirectory
parameter:The example will create a
Configuration
class which sets theMigrationsDirectory
to the specified folder 'Migrations\CustomerDatabases' which is relative to the projects root folder.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 youDbContext
derived class (where the Fluent-API configuration is).This will work in EF6:
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
).