dotNET dotNET - 2 months ago 19
ASP.NET (C#) Question

Adding custom columns to ASP.NET Identity

Trying to grasp the big picture here. This is a Web Forms project using Identity + EF for user management. The default project contains

IdentityModels
file which suggests that I should add custom properties to
ApplicationUser
class and then use migrations to update my database. I did that and the database was generated/updated. So far so good.

Now I add a new EDMX to my project and import all my DB tables into it. This obviously brings in Identity tables into the diagram as well, which is good because I'll be adding my business domain tables and linking them to Identity tables through the model and then use migrations to update my database. Here are the questions and problems I face:


  1. Am I using Code-First or Model-First, or a mix of both (Does such a mix work)?

  2. Do I have more than one model in my project, namely the default Models file and the one generated by EDMX?

  3. If I have two models, which of the model classes correspond to
    AspnetUsers
    table; the default
    ApplicationUser
    class or the
    AspNetUser
    class generated by the EDMX? I mean which of these classes will be used by migrations to update my table's structure?

  4. Adding new properties to my
    ApplicationUser
    class doesn't seem to have any effect when I run
    Add-Migration
    and
    Update-database
    commands. It generates empty
    Up()
    and
    Down()
    functions.

  5. Adding a new property to an EDMX entity and then trying to send it to the database through migrations throws error saying that the new property doesn't have a mapping column. Now that's obvious I know, but then how does Model-First approach send changes to the DB?



I know these are more than one questions, but they are tightly related and anyone trying to get a start will most probably face all of them, so I've gathered them in one place for future readers.

Answer

OK. After working with the project for a few days, I have figured out a few things that might be helpful for future readers:

  1. As @Konstantin said, as a general rule, you should not use both code-first and model-first approaches in the same project. Personally I prefer database-first over both of them, i.e. create a database design and then import it into my EDMX model. I can then make changes to my DB design later and use "Update Model from Database..." command to refresh my model.

  2. AFAIK, migrations cannot currently be used with EDMX models. These only work with code-first approach.

  3. ASP.NET Identity will automatically create all required tables in your database when your website runs for the first time. You simply need to correct the connection string in your web.config file.

  4. You should generally avoid bringing in Identity tables into your EDMX, but if you really need to do that, do not make changes to these entities through EDMX. Simply use ApplicationUser class in IdentityModels file to add custom properties to your user class.