How to mark identity column properly with Entity Framework 6.1?

后端 未结 3 1726
滥情空心
滥情空心 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:07

    This is for those who lost more than two hours on this issue.

    I made a mistake by marking a column as KEY that later I realize it was not the key, but just a column.

    My model had dependencies, so I could not just remove the KEY attribute and let it be. The default Id column never got as identity column at SQL Server, either after generating the corresponding migration and setting: identity as true.

    Instead, I had to remove the Dbset from context, remove all references to it and from it in other entities. And generate a migration that drops the table, then fixing the model.. make sure it was OK. And then generating the well done model, plugging in all dependencies back.

    It was my mistake on the first hand. So I have no body to blame but myself.

    Hope no one come to this situation again, but if do happens, this what I did.

提交回复
热议问题