EntityFramework code first: Set order of fields

后端 未结 4 916
梦毁少年i
梦毁少年i 2020-12-09 08:26

I am using EntityFramework with the \"Code first\" approach with migrations.

I have successfully generated tables from my models, but the columns are being added in

4条回答
  •  不知归路
    2020-12-09 09:07

    At this moment EF core doesn't support it.But there is a workaround for that.That is, you can explicitly specify SQL on your migration operation.

    Instead of using the CreateTable method in your migrations, you need to explicitly write the SQL as shown below.There you can give the order as you wish.

    migrationBuilder.Sql("CREATE TABLE Properties(
       MyFirstKeyProperty   INT   NOT NULL,
       MySecondKeyProperty int    NOT NULL,
       AGE  INT   NOT NULL,
       ......
       ......   
       PRIMARY KEY (MyFirstKeyProperty)
    )");
    

    You can read about the rowanmiller's commnet here about how to sort out that issue just for now

提交回复
热议问题