Code First. Column Order Annotation

Working with Code First Entity Model, you can set the order of columns in database table. It’s very easy – just add an annotation before property declaration.

But there are some interesting things about this annotation.
1. Order numbers are relative, they are not the exact places of the fields inside the database table. For example, you can set 1 for colA, 10 for colB, 5 for colC, and you will get the following order: colA, colC, colB.
2. If you don’t set order numbers for all columns, Visual Studio will place ordered columns first (according to order numbers), then the rest columns (without order numbers).
3. It works only at the moment when Visual Studio creates a table. If you change the annotation later, migration does not generate code for table recreation.

Leave a Reply

Your email address will not be published. Required fields are marked *