Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ASP.NET MVC, move Authentication Models to a Class Library

I am working on a new project in ASP.NET MVC 5 with Entity Framework 6 Code First.

CURRENT STATE

I have two projects within one solution. One is the Front-End (MVC) and the other is a Class Library that I hope would house all my Model classes (Not ViewModels).

As we all know, when you start a new MVC project, the Identity Models sit within the [Models] folder. Since these logically belong in my Class Library project, I would like to move them there.

solution explorer

WHAT I HAVE TRIED

Inside the [Authentication] folder I have gone ahead and created an ApplicationUser.cs class. Now by simply looking at IdentityModels.cs I know that I would need to reference Microsoft.AspNet.Identity among other things.

So I right click on the Reference for the BB.DOMAIN project and click {Add Reference}. But I can't see the Microsoft.AspNet.Identity library anywhere!

adding references

CLARIFICATION REQUIRED

Am I doing the right thing here? My current thinking is that I am doing something one shouldn't typically do! But then everywhere you read, you get told that all your core entities should typically sit in one place.

Another thing I am confused about already but haven't got to is this:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

When I get to add ApplicationDbContext.cs to my [Authentication] folder in BB.DOMAIN. In order to make everything sit in the same database, do I simply change base("DefaultConnection") to whatever the name of my connection string is in my other DbContext class which sits inside the [Context] folder?

I want all my entities driving this application to sit in one database.

like image 461
J86 Avatar asked Sep 29 '22 21:09

J86


1 Answers

The super class IdentityUser which used in IdentityModel file came from Microsoft ASP.NET Identity EntityFramework package, not part of .Net Framework 4.5.

You need to add this nuget package and dependencies to the project BB.DOMAIN.

like image 195
silverfox Avatar answered Oct 06 '22 20:10

silverfox