ASP.NET Identity 2.0 Extension

I had problems understanding the whole ASP.Net Identity framework, I first tried to integrate Identity 1.0 into an existing application, but in the end I had 2 DbContext classes, my application data was in MyDbContext and Identity user data in IdentityDbContext.

I want to use one DbContext for both my application data and for identification related data, and I found on another issue that I can use IdentityDbContext the same way I use DbContext; however, I think I missed something.

I am using a sample project from the Identity team

PM> Install-Package Microsoft.AspNet.Identity.Samples -Pre 

and tried to insert DbSet properties in ApplicationDbContext

  public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext() : base("DefaultConnection") { } static ApplicationDbContext() { // Set the database intializer which is run once during application start // This seeds the database with admin user credentials and admin role Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer()); } public virtual DbSet<Student> students { get; set; } public virtual DbSet<Teachers> teachers { get; set; } public static ApplicationDbContext Create() { return new ApplicationDbContext(); } } 

I use the ApplicationDbInitializer seed method to populate these two tables, for example:

 Student s = new Student { Name = "John" ... }; db.students.Add(s); db.SaveChanges(); 

But EF does not seem to create these 2 tables, and the seed method is not called, what happens?

EDIT:

Well, I need to run code that uses the database for EF to delete and recreate the database.

Is there any overhead when using IdentityDbContext to manage all the data in my application? What is the difference between using IdentityDbContext and using the generic version of IdentityDbContext <IdentityUser>?

+6
source share
1 answer

In most cases, it is preferable to use only one Entity Framework context, since then you will need to manage only one, which means less overhead.

The difference between the two is that IdentityDbContext is just a class that comes from DbContext (as when creating your own), but it contains additional DbSets that deal with authentication, so you don't need to implement it yourself. Getting from IdentityDbContext great. For more information, you can read the documentation .

+4
source

Source: https://habr.com/ru/post/976927/


All Articles