0

Currently, I am working to update/modify columns order by modifying code in ef core 6 project only. I have researched and found annotation [Column(order=0)] or [Column(order=1)] ... . But exactly if it works only first migration, for the next migrations by modifying column order in the model, the database doesn't update the following migration.

Model at first:

public class Student
{
    [Column(Order=0)]
    public int Id {get; set;}

    [Column(Order = 1)]
    public string Name {get; set;}

    [Column(Order = 2)]
    public DateTime Created_at {get; set;}

}
+----+------+------------+
| Id | Name | Created_at |
+----+------+------------+

After I add the new property "Age" into the model by setting column order=2 and "Created_at" 's order=3 but in the database, it displays "Age" at the last which is not my expectation.

Latest Model:

public class Student
{
    [Column(Order=0)]
    public int Id {get; set;}

    [Column(Order = 1)]
    public string Name {get; set;}

    [Column(Order = 2)]
    public int Age {get; set;}

    [Column(Order = 3)]
    public DateTime Created_at {get; set;}

}
+----+------+------------+-----+
| Id | Name | Created_at | Age |
+----+------+------------+-----+

While expectation is:

+----+------+-----+------------+
| Id | Name | Age | Created_at |
+----+------+-----+------------+

Provider and version information

Database provider: Pomelo.EntityFrameworkCore.MySql

Target framework: .NET 6.0

IDE: Visual Studio 2019 16.3

Rotnak
  • 1
  • 2
  • Does this answer your question? [Possible to set column ordering in Entity Framework](https://stackoverflow.com/questions/43246727/possible-to-set-column-ordering-in-entity-framework) – IamK Oct 03 '22 at 07:12
  • Does this answer your question? [How to change the column position of MySQL table without losing column data?](https://stackoverflow.com/questions/10718905/how-to-change-the-column-position-of-mysql-table-without-losing-column-data) – Svyatoslav Danyliv Oct 03 '22 at 07:45
  • You cannot change column order by EF Core for existing tables. You can do that by yourself by executing scripts. – Svyatoslav Danyliv Oct 03 '22 at 07:46
  • Upload the images directly to the question. External image links are not preferred and sometimes ignored by readers. – Ibrahim Timimi Oct 03 '22 at 11:32

1 Answers1

0

The only way to change the order of the columns is to recreate the table. But it's really not worth it - the order of the columns in the table doesn't make any difference to performance, or to the behavior of Entity Framework. The only time it would make any difference would be if you issued a SELECT * FROM [Table.Name] query, which is a bad idea anyway.

Use the zero-based Order parameter to set the order of columns in the database. As per the default convention, PK columns will come first and then the rest of the columns based on the order of their corresponding properties in an entity class.

Note: The Order parameter must be applied on all the properties with a different index, starting from zero.

Sample Code Project

Thanks You!

Rahul Parab
  • 59
  • 10