How to mark identity column properly with Entity Framework 6.1?

后端 未结 3 1723
滥情空心
滥情空心 2021-01-04 16:29

I\'ve seen many posts and answers regarding how to mark a field as the identity column. Many of them are outdated and are targeting older versions of Entity Framework.

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-04 17:23

    As long as the type of the primary key property is numeric or GUID, Code First will, by convention, automatically configure the key as an identity column.

    That means you don't need to have any of the configuration you put in your code to explicity set the property as an identity column because Code First already use covention for that. The data annotation attribute or fluent API configurations you set are useless.

    You use those configurations on numeric or GUID type primary key only if you want to disable the identity.

提交回复
热议问题