如何通过实体框架代码首次迁移影响列顺序 [英] How to affect the column order with Entity Framework Code First Migrations

查看:159
本文介绍了如何通过实体框架代码首次迁移影响列顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework 4.3 Code First,并尝试迁移功能。

I'm using Entity Framework 4.3 Code First and trying out the Migrations feature.

如果我向我的课堂添加一个新的属性,然后运行Add-Migration from包管理器控制台窗口我得到这样的东西:

If I add a new property to my class and then run Add-Migration from the package manager console window I get something like this:

    public override void Up()
    {
        AddColumn("Products", "Discontinued", c => c.Boolean(nullable: false));
    }

我希望能够影响列的顺序,因为我不希望它被附加到表中,而是放在特定的索引上。我以为我可以将它添加到我的modelBuilder配置中,例如:

I would like to be able to affect the order of the column as I don't want it to just be appended to the table but rather placed at a specific index. I thought I might be able to add it to my modelBuilder configuration, something like:

Property(p => p.Discontinued).HasColumnOrder(2);

但运行更新数据库似乎没有使用它。这可以做为迁移吗?

but running Update-database does not appear to use it. Can this be done as a migration?

推荐答案

这只是一个缺少功能的问题。 SQL本身不依赖于列的任何隐式顺序(除了一些例外:ORDER BY,...)。

This is just a matter of missing functionality. SQL by itself does not rely on any implicit order of columns (with some exceptions: ORDER BY , ...).

SQL Server和ORACLE都没有直接的SQL DDL命令(又名 ALTER TABLE ... )来移动一列。

Neither SQL Server nor ORACLE do have a direct SQL DDL command (aka ALTER TABLE...) to move a column around.

因此,无需高昂的努力就可以更改订单(重新创建表)。参见例如

Therefore there's no possibility to change the order without high effort (recreate the table). See for example

  • How To change the column order of An Existing Table in SQL Server 2008
  • SQL SERVER – Change Order of Column In Database Tables
  • https://dba.stackexchange.com/questions/61978/how-to-change-the-column-order

这篇关于如何通过实体框架代码首次迁移影响列顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆