可以在Entity Framework中设置列排序

Ish*_*ohn 7 c# entity-framework ef-code-first entity-framework-6 ef-fluent-api

是否有任何可能的配置来设置实体框架代码中的数据库列排序第一种方法..?

我的所有实体集都应该有一些用于保存recordinfo的公共字段

public DateTime CreatedAt { get; set; }
public int CreatedBy { get; set; }
public DateTime ModifiedAt { get; set; }
public int ModifiedBy { get; set; }
public bool IsDeleted { get; set; }
Run Code Online (Sandbox Code Playgroud)

我希望将这些字段保留在表的末尾.是否有任何可能的EF配置可用于配置此配置,而不是将此字段保留在我的模型类的末尾.

Ste*_*eve 6

我假设您正在使用Entity Framework 6,因为EF Core中尚不支持列排序。

您可以使用数据属性或流畅的API来设置列顺序。

要使用数据属性设置列顺序,请参考System.ComponentModel.DataAnnotations并使用ColumnAttribute。如果希望它与属性名称不同,也可以使用此属性设置列名称。

[Column("CreatedAt", Order=0)]
public DateTime CreatedAt { get; set; }
[Column("CreatedBy", Order=1)]
public int CreatedBy { get; set; }
Run Code Online (Sandbox Code Playgroud)

请注意,Order参数从零开始。

另请参阅:http : //www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-first.aspx

另外,您可以OnModelCreating在DbContext类的方法中使用Fluent API :

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    //Configure Column
    modelBuilder.Entity<EntityClass>()
                .Property(p => p.CreatedAt)
                .HasColumnOrder(0);
}
Run Code Online (Sandbox Code Playgroud)

另请参阅:http : //www.entityframeworktutorial.net/code-first/configure-property-mappings-using-fluent-api.aspx

这种方式比较冗长,但是您可以对发生的事情有更多的控制。