Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering Column Attributes in Entity Framework

I am using Entity Framework in my application,so I have mapped my property objects to database objects. The property objects have been defined using Column attribute.

Now,I want to order them,but I can't find the property in Column Attribute for Order.

I have included System.ComponentModel.DataAnnotations,but still not getting it

Thanks in advance

like image 566
Channakeshav Avatar asked Oct 10 '14 15:10

Channakeshav


People also ask

What is column order in Entity Framework?

Column order By default when creating a table with Migrations, EF Core orders primary key columns first, followed by properties of the entity type and owned types, and finally properties from base types. You can, however, specify a different column order: Data Annotations. Fluent API.

Which Namepace should be included in order to Overrride the conventions using attributes in Entity Framework Code First apporach to generate the database?

Column. Column attribute overrides the default convention. EF Code First will create a column with a specified name in the Column attribute for a given property.

What is System ComponentModel DataAnnotations?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.

Which annotation is used when the name of attributes are different from the column names?

Use data annotations to use the different table and column names.


1 Answers

just use :

using System.ComponentModel.DataAnnotations.Schema;

Code :

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

Notice : cloumn arder by default takes a big number so if you ordered only this column it will be first one in the table unless you ordered another column with a lower order number which is in this case : 0

like image 173
noor saif Avatar answered Oct 02 '22 20:10

noor saif