Order columns when creating a database using Entity Framework 4.1. with white API

I want to have the column order of the database table after creating the database using the 4.1 framework. in the following way:

1) PK 2) all columns of a foreign key 3) all columns of complex types 4) all other columns

The problem is that there is no way to set an order for a foreign key using a free API, such as HasColumnOrder for primitive properties. (all columns of the foreign key are the last columns)

Any ideas?

thanks

Chris

+4
source share
4 answers

I know this is an old thread, but I thought I would actually respond. I agree, the order of the columns in the database does not matter. Unless you are looking for a database on data, then it may be useful to have logical / useful fields first.

The answer lies in the .HasColumnOrder (x) method. Just enter a zero number of the order you want the field to be. See an example here: http://hanksnh.wordpress.com/2011/04/08/inheritance-with-entity-framework-4-1/

+2
source

If you want to set the order for foreign key columns, you must represent them as properties (use foreign key association instead of independent association ). Btw. the order of the columns in the database table does not matter. Only the order of the columns in the key matters, and therefore there is no brother support for this.

+1
source

I solved the problem with the order of the foreign key columns in the database when using independent association using EF migration.

Turn off automatic migration and create an initial migration for your data model.

For model class:

public class Session { public int? Id { get; set; } public Track Track { get; set; } public Car Car { get; set; } public int Event { get; set; } public DateTime Date { get; set; } public string Name { get; set; } } 

migration code will be generated:

 CreateTable("dbo.Sessions", c => new { Id = c.Int(nullable: false, identity: true), Event = c.Int(nullable: false), Date = c.DateTime(nullable: false), Name = c.String(nullable: false, maxLength: 64), Car = c.Int(nullable: false), Track = c.Int(nullable: false), }) ... 

Now just rearrange the columns of the foreign key (car, track) by moving them up. When you create a new database and open the table, the column order will be as expected.

0
source

If you are looking for column order, I think this is pretty easy. In your DbContext class, override OnModelCreating. Then grab modelBuilder, and from there pull out EntityTypeConfiguration. Then, using it, adjust the order as follows.

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

Finally, you need to add the migration, and then update the database.

0
source

All Articles