Search code examples
c#asp.netentity-frameworkwebformsasp.net-identity

C# Common DbContext for ASP.NET Identity with webforms


I'm beginner to .NET but I'm fairly familiar with building webapplications using 3 tier architecture. I usually have a webapplication project for the webforms and a wpf application project to keep my Domain, Controller and Data Access Layer classes.

I'm familiar with creating a DbContext class and using it to deal with the database through EF. I've added .NET Identity to my webapplication project but I need to make changes to the identity user and add more data to it.

I'm following this tutorial to accomplish this but it is intended for MVC. I have 2 seperate context files at the moment, one for identity and other for my usual purposes. is there a way I can have one context?

my DbContext looks like,

    namespace ExammerCore.Infrastructure
{
    class ExammerContext : DbContext
    {
        public ExammerContext()
            : base("DefaultConnection")
        {
            
        }
    }
}

the identity DbContext I made using the tutorial looks like,

namespace ExammerCore.Infrastructure
{
    class IdentityContext : IdentityDbContext<ApplicationUser>
    {
        public IdentityContext()
            : base("DefaultConnection")
        {
        }
    }
}

My IdentityModels.cs looks like,

namespace ExammerCore.Domain
{
    public class ApplicationUser : IdentityUser
    {

        //You can extend this class by adding additional fields like Birthday
        public string BirthDate { get; set; }

    }

}

how can I blend these two together to have one DbContext class? or is there another way to add custom fields to a user without inheriting Identity classes? My solution structure looks like this, Solution structure

I've googled a lot haven't found an answer.


Solution

  • Change the line

    class ExammerContext : DbContext
    

    to

    class ExammerContext : IdentityDbContext<ApplicationUser>
    

    to pull all the Identity DbSets into your ExammerContext