Changing the SQL Server Alphabetical Entry in Entity Framework Code - First to Sequential

I need to disable the alphabetical order in the code first.

Here is my class simplified

public class Person
{
    [Key,Column("PersonId")]
    public int Id { get; set; }
    [MaxLength(50)]
    public string PersonName{ get; set; }
    public DateTime? JoinDate{ get; set; }
    public int? Gender{ get; set; }
}

and when I run the commands to create the database

dnx ef migrations add InitialMigration
dnx ef  database update

The columns of the database, except the primary key, are generated in alphabetical order when I view it in design mode in SQL Server 2012.

How to make it create columns in sequential order as it appears in the class.

I looked on github and could find this question that does not explain how to disable it.

+4
source share
2 answers

EF7- . , SQL .

, "CreateTable" SQL.

migrationBuilder.Sql("CREATE TABLE Person ...");
+3

, , . DbContext OnModelCreating. modelBuilder, EntityTypeConfiguration. , , .

public class AppDbContext : IdentityDbContext<AppUser, AppRole, int, AppUserLogin, AppUserRole, AppUserClaim>
{
    public AppDbContext() : base("AvbhHis")
    {

    }

    public DbSet<PatientCategory> Product { get; set; }
    public DbSet<LookupBase> LookupBase { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder
            .Entity<PatientCategoryLookup>()
            .Map<PatientCategoryLookup>(m =>
            {
                m.ToTable("LookupPatientCategory");
                m.MapInheritedProperties();
            });
        EntityTypeConfiguration<PatientCategoryLookup> config = modelBuilder.Entity<PatientCategoryLookup>();
        config.Property(e => e.Id).HasColumnOrder(0);
        config.Property(e => e.PatientCatgCode).HasColumnOrder(1);
        config.Property(e => e.PatientCatgName).HasColumnOrder(2);
        config.Property(e => e.Description).HasColumnOrder(3);
        config.Property(e => e.ModifiedTime).HasColumnOrder(4);
        config.Property(e => e.History).HasColumnOrder(5);

        base.OnModelCreating(modelBuilder);
    }

}

, , , .

-1

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


All Articles