locked
How to Integrating ASP.NET Identity into Existing DbContext RRS feed

  • Question

  • i want identity should use my own DB context instead of its own. please guide me how to do it.

    i want single db context through out the project. guide me how it will be possible ?

    sample identity db context

    public class ApplicationDbContext : IdentityDbContext
        {    
            public ApplicationDbContext(): base("DefaultConnection")
            {
            }
    
            protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
                modelBuilder.Entity<IdentityUser>().ToTable("user");
                modelBuilder.Entity<ApplicationUser>().ToTable("user");
    
                modelBuilder.Entity<IdentityRole>().ToTable("role");
                modelBuilder.Entity<IdentityUserRole>().ToTable("userrole");
                modelBuilder.Entity<IdentityUserClaim>().ToTable("userclaim");
                modelBuilder.Entity<IdentityUserLogin>().ToTable("userlogin");
            }
        }

    and one sample of my own db context

    public class TestDBContext : DbContext
    {
    	public TestDBContext()
    	    : base("name=TestDBContext")
    	{
    
    	}
    
    	protected override void OnModelCreating(DbModelBuilder modelBuilder)
    	{
    	    //modelBuilder.Configurations.Add(new vwCustomerConfiguration());
    	    Database.SetInitializer<TestDBContext>(null);
    	}
    
    	public DbSet<Customer> Customer { get; set; }
    	public DbSet<Addresses> Addresses { get; set; }
    	public DbSet<Contacts> Contacts { get; set; }
    	public virtual DbSet<vwCustomer> vwCustomers { get; set; }
    	public DbSet<vwMyCustomers> vwMyCustomers { get; set; }
    }
    now tell me how could i merge two db context in one.

    thanks

    Thursday, November 17, 2016 9:26 AM

Answers

  • Hi Mou_kolkata,

    >>i want identity should use my own DB context instead of its own. please guide me how to do it.

    IdentityDbContext is a class which has inherited from DbContext. If you want to use your own DB context instead of its own, just use a own Context class inheriting from IdentityDbContext, then  your own DbContext will have the context any relations between your classes and the IdentityUser and Roles of the IdentityDbContext. For more information, please refer to:

    ASP.NET Identity DbContext confusion

    Integrating ASP.NET Identity into Existing DbContext

    Merge MyDbContext with IdentityDbContext

    I hope this can be helpful.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mou_kolkata Monday, December 26, 2016 6:17 PM
    Friday, November 18, 2016 3:00 AM

All replies

  • Hi Mou_kolkata,

    >>i want identity should use my own DB context instead of its own. please guide me how to do it.

    IdentityDbContext is a class which has inherited from DbContext. If you want to use your own DB context instead of its own, just use a own Context class inheriting from IdentityDbContext, then  your own DbContext will have the context any relations between your classes and the IdentityUser and Roles of the IdentityDbContext. For more information, please refer to:

    ASP.NET Identity DbContext confusion

    Integrating ASP.NET Identity into Existing DbContext

    Merge MyDbContext with IdentityDbContext

    I hope this can be helpful.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Mou_kolkata Monday, December 26, 2016 6:17 PM
    Friday, November 18, 2016 3:00 AM
  • would u plzz come with a samll but complete code which show a single context can handle identity related things and other things too.
    Friday, November 18, 2016 11:59 AM