Entity Framework 6 如何訂購遷移

盜龍

當 db 上下文配置為 時,遷移的順序是MigrateDatabaseToLatestVersion什麼?自動生成的遷移類似乎不包含任何排序。唯一似乎對訂單有線索的地方是遷移文件名/時間戳——但這在運行時似乎不太可能或不可用......對嗎?

漢克·麥考德

初始遷移名稱實際使用的東西。這也與初始文件名相同,但您顯然可以更改它。

在 EntityFramework 中,當您生成遷移時,您可能熟悉編輯文件以自定義遷移。

    public partial class InitialCreate : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Blogs",
                c => new
                    {
                        BlogId = c.Int(nullable: false, identity: true),
                        Name = c.String(),
                    })
                .PrimaryKey(t => t.BlogId);
            
        }
        
        public override void Down()
        {
            DropTable("dbo.Blogs");
        }
    }

但是還會生成一個設計器文件,它有一些您不應編輯的屬性

[GeneratedCode("EntityFramework.Migrations", "6.4.4")]
public sealed partial class InitialCreate : IMigrationMetadata
{
    private readonly ResourceManager Resources = new ResourceManager(typeof(InitialCreate));
        
    string IMigrationMetadata.Id
    {
        get { return "202111200213248_InitialCreate"; }
    }
        
    string IMigrationMetadata.Source
    {
        get { return null; }
    }
        
    string IMigrationMetadata.Target
    {
        get { return Resources.GetString("Target"); }
    }
}

EF6源代碼中,您可以看到遷移反映在您的遷移程序
集中DbMigration:使用繼承自的每個類,然後按Id屬性排序

_migrations
    = (from t in migrationsAssembly.GetAccessibleTypes()
        where t.IsSubclassOf(typeof(DbMigration))
                && typeof(IMigrationMetadata).IsAssignableFrom(t)
                && t.GetPublicConstructor() != null
                && !t.IsAbstract()
                && !t.IsGenericType()
                && t.Namespace == migrationsNamespace
        select (IMigrationMetadata)Activator.CreateInstance(t))
        .Where(mm => !string.IsNullOrWhiteSpace(mm.Id) && mm.Id.IsValidMigrationId())
        .OrderBy(mm => mm.Id)
        .ToList();

即使您重命名文件,在Id創建遷移時仍將具有其初始值,按照 EF6 所見的詞法時間順序保持遷移

EFCore 幾乎相同,只是它已從設計器文件上的屬性移動到屬性:

    [DbContext(typeof(MyContext))]
    [Migration("20211118001328_InititialCreate")]
    partial class Init {}

EFCore查找,從繼承的類,屬性Migration

var items
    = from t in Assembly.GetConstructibleTypes()
        where t.IsSubclassOf(typeof(Migration))
            && t.GetCustomAttribute<DbContextAttribute>()?.ContextType == _contextType
        let id = t.GetCustomAttribute<MigrationAttribute>()?.Id
        orderby id
        select (id, t);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何模拟Entity Framework 6异步方法?

如何在 Entity Framework Core 中生成空遷移?

EntityDataSource和Entity Framework 6

如何使用.include查询强制外部联接Entity Framework 6

如何使用Entity Framework 6创建内存DbContext?

如何使Entity Framework 6在CSDL中使用SQL STUFF函数?

如何将Entity Framework 6绑定到KendoUI Grid

SQLite Entity Framework 6提供程序如何处理Guid?

如何使用Entity Framework 6调用SQL Server过程

如何在Entity Framework 6中删除多对多关系

如何在Asp.Net 5(MVC 6)中使用Entity Framework 6.x

Entity Framework v6 中的 TPC

在Entity Framework 6中更新子对象

Entity Framework 6产生的效率低下的查询

从Entity Framework 6中的集合中删除

如何使用Entity Framework 6从存储过程中检索输出参数

如何使用Entity Framework 6在具有多个外键的实体上应用级联删除?

如何链接指向Entity Framework 6上现有DBSet源的数据?

人们应该如何使用Entity Framework 6进行单元测试?

如何避免Entity Framework 6在生成的查询中添加ORDER BY

如何使用Entity Framework 6从10多个表中获取记录并显示其中的报告?

如何使Entity Framework 6(数据库优先)显式插入Guid / UniqueIdentifier主键?

如何从Entity Framework 6(首先是数据库)和Npgsql执行存储过程?

如何在LEFT JOIN中的Entity Framework 6中生成IS NULL和IS NOT NULL

如何在Entity Framework 6中混合使用TPH和TPT?

如何使用Entity Framework 6动态连接到其他数据库?

如何将带有 EntityState 和值的查询从 Entity Framework 5 转换为 6?

如何在Entity Framework -6中的onmodelcreating方法中映射与模型不同的表名和列名?

如何在Entity Framework 6中设置默认值(数据库优先)