是否可以将每个模型的迁移分离到各自的自定义文件夹中

时间:2019-03-24 22:38:31

标签: entity-framework

我想将每个模型迁移文件放在其自己目录下的实体文件夹下,而不是根文件夹下(迁移)

Note > Entities > Migrations

Contacts > Entities > Migrations

所以,我的文件夹结构如下:

Notes
    - Entities
        - Note
    - Http
        - Handlers
            - ShowNote.cs

Contacts
    - Entities
        - Contact
    - Http
        - Handlers
            - ShowContact.cs

现在,我两个实体之间都有关系,hasOne,我有一个数据库上下文,如下所示:

using Microsoft.EntityFrameworkCore;
using software.Contacts.Entities;
using software.Notes.Entities;

namespace software.Core.Entities
{
    public class SoftwareContext : DbContext
    {
        /// <inheritdoc />
        /// <summary>
        /// Constructor
        /// </summary>
        public SoftwareContext(DbContextOptions options)
            : base(options)
        { }

        /// <summary>
        /// Contact model
        /// </summary>
        public DbSet<Contact> Contact { get; set; }

        /// <summary>
        /// Note model
        /// </summary>
        public DbSet<Note> Note { get; set; }
    }
}

当我运行迁移时,它会在根目录中创建一个文件夹,但是我试图使迁移进入上述的相应文件夹,是否有可能在没有为每个模型创建单独的数据库上下文的情况下实现?

例如,是否有任何类似的代码可以实际工作:

using Microsoft.EntityFrameworkCore;
using software.Contacts.Entities;
using software.Notes.Entities;

namespace software.Core.Entities
{
    public class SoftwareContext : DbContext
    {
        /// <inheritdoc />
        /// <summary>
        /// Constructor
        /// </summary>
        public SoftwareContext(DbContextOptions options)
            : base(options)
        { }

        /// <summary>
        /// Contact model
        /// </summary>
        [MigrationsDirectory("{root}/Contact/Entities/Migrations")]
        public DbSet<Contact> Contact { get; set; }

        /// <summary>
        /// Note model
        /// </summary>
        [MigrationsDirectory("{root}/Note/Entities/Migrations")]
        public DbSet<Note> Note { get; set; }
    }
}

1 个答案:

答案 0 :(得分:0)

一种方法是在添加新迁移时仅指定迁移路径,如下所示

Add-Migration -o Data\Migrations

我相信您可以使用以下命令设置迁移路径,但这会影响以后的所有迁移

enable-migrations -EnableAutomaticMigration:$false -MigrationsDirectory yourdirectory