It is possible to set the column order in the Entity Framework

Is there any possible configuration for setting the column order of the database in the first approach of the entity structure code.?

My entire set of objects should have some common fields for storing 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; }

I want to keep these fields at the end of the table. Is there any possible EF configuration that I can use to configure it, and not to save these fields at the end of my model class.

+4
source share
2 answers

, 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; }

, "" .

: http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-first.aspx

Fluent API OnModelCreating DbContext:

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

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

, , .

+4

:

using System.ComponentModel.DataAnnotations.Schema;

:

[DisplayColumn("Name" , Order = 1)]
 public int UserName { get; set; }

: cloumn arder , , , , , : 0 !

0

Source: https://habr.com/ru/post/1674166/


All Articles