4

When you create a new MVC5 project in VS2013 with Individual User Accounts, the account tables (dbo.AspNet*) are created automatically in the database defined by the DefaultConnection the first time you try to log in or register a new account. Does anyone know the exact class/method in the project that creates these tables?

With SimpleMembership in VS2012 / MVC4, the tables were created using this method:

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

which is called in the SimpleMembershipInitializer class of the InitializeSimpleMembershipAttribute.cs filter.

I have not been able to find a similar method in a VS2013 / MVC5 project.

Any clues?

M.Reisner
  • 41
  • 3
  • There isn't a method like that in ASP.NET Identity. There are other ways to customize ASP.NET Identity. Are you looking for this method because you want to customize the way ASP.NET Identity works? If you ask how to perform this type of customization you may get more helpful answers. – Kevin Junghans Dec 13 '13 at 13:43
  • 1
    At this point I'm simply looking for the method that creates the Identity tables because I want to understand how every little bit works. – M.Reisner Dec 14 '13 at 06:11

2 Answers2

1

By default, the MVC5 template relies on EntityFramework CodeFirst's default database initializer to create the database the first time its needed.

See CreateDatabaseIfNotExists

Hao Kung
  • 28,040
  • 6
  • 84
  • 93
  • Hao, can you explain further? I'm still confused since I cannot find the CreateDatabaseIfNotExists method call anywhere in the project. – M.Reisner Dec 18 '13 at 07:46
  • There is no method call in the project, this is done by EntityFramework automatically when it needs to, the first time a query is done. If you want to customize this, you need to plug in your own initializer, or you can hook/override the OnModelCreating method in your DbContext – Hao Kung Dec 18 '13 at 19:09
0

Have a look at the DbContext (System.Data.Entity), inherited by IdentityDbContext (Microsoft.AspNet.Identity.EntityFramework), inherited by ApplicationDbContext that you will find inside the IdentityModels.cs in the default Visual Studio 2013 ASP.NET Web Application/MVC template.

The AccountController gives you a clue as to how the above gets into play in how it is initialized:

public AccountController()
        : this(new UserManager<ApplicationUser>(
              new UserStore<ApplicationUser>(new ApplicationDbContext()))) {}

(If, by any chance, you're looking to customize, Kevin is right in his comment. For example, properties can be easily added to User through the ApplicationUser, and more control can be achieved as usual by your own implementation of whatever. The old identity and authentication architecture has been completely replaced by OWIN/Katana.)

EDIT: Actually, I might have slightly misread your question. The code that actually creates the data base if need be probably resides in something like the CreateDatabaseIfNotExists class (System.Data.Entity). I'm not sure which exact implementation gets called from which context, but obviously creating the DB is the responsibility of Entity Framework.

Still, using dotPeek, ILSpy, ReSharper or something similar, you should be able to find what you're looking for if you start out exploring the classes I mentioned initially. (The suggestion that this is not the place to alter behavior still stands.)

Hope this helps.

Oskar Lindberg
  • 2,255
  • 2
  • 16
  • 36
  • Oskar, As I said to Hao above, I haven't found the CreateDatabaseIfNotExists method call anywhere in the project, so I'm still in the dark as to how what method call is creating the database and tables. I used SQL Profiler while stepping through the code, and saw that it was all created after a call to UserManager.CreateAsync, but that's to create a new user. Drilling down into the various types and classes related to UserManager has not revealed a call to CreateDatabaseIfNotExists. – M.Reisner Dec 18 '13 at 07:55