简体   繁体   中英

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.

Change the line

class ExammerContext : DbContext

to

class ExammerContext : IdentityDbContext<ApplicationUser>

to pull all the Identity DbSets into your ExammerContext

in your dbcontext you should have properties that represent what entities get used to build tables.... that should look something like this...

class ExammerContext : DbContext
{
    public IDbSet<ApplicationUser> ApplicationUsers { get; set; }
    // add other classes/tables here.

    public ExammerContext()
        : base("DefaultConnection")
    {

    }
}

this way you will only need your context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM