Reputation: 35400
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:
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?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.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.
Upvotes: 1
Views: 4314
Reputation: 35400
OK. After working with the project for a few days, I have figured out a few things that might be helpful for future readers:
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.
AFAIK, migrations cannot currently be used with EDMX models. These only work with code-first approach.
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.
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.
Upvotes: 1
Reputation: 38378
In my understanding using both EF Code-First and Model-First can add a burden of keeping them in sync. You may want to check the following sample project which uses only DbFirst approach:
https://github.com/kriasoft/AspNet-Server-Template
Upvotes: 1