Add column to aspnetusers with database first

2020-04-05 11:21发布

I have found many tutorials for adding columns to the Identity tables (which I have successfully moved to the application database) with database migrations however my understanding is this is not applicable in database fist projects. So...how do I add columns to the aspnetusers table in a database first project?

I would like to ad a bit type column called Is RegComplete which is initially set to 0 then at some point when the user has completed some more tasks then set to 1.

2条回答
贪生不怕死
2楼-- · 2020-04-05 12:17

OK, I've cracked it! Firstly I didn't realise that although I have moved the Identity tables to the Application database there is still two Database Contexts, one for the application tables which are DB First, and the other for the Identities tables.

I was able to enable migrations and add the column using code first and migrate then update the database. The new column is now available in the controller.

I found this tutorial which helped me: http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx

查看更多
时光不老,我们不散
3楼-- · 2020-04-05 12:17

The easiest solution:

  • Add columns into AspNetUsers table
  • Add properties into IdentityModels.cs class (Check attachment)
  • Add same properties into AccountViewModels.cs\RegisterViewModel class
  • Compile and it will work.

Attachment enter image description here

(VS 2017, MVC5)

查看更多
登录 后发表回答