dotNET dotNET - 1 year ago 121
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

file which suggests that I should add custom properties to
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
    table; the default
    class or the
    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
    class doesn't seem to have any effect when I run
    commands. It generates empty

  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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download