Use the same database for both my custom entities and identity context?

≡放荡痞女 提交于 2019-12-04 20:48:49

问题


I want to make an MVC website with user logins/authentication etc.

I'm using EF CodeFirst to create my database.

If I create a new MVC project and select Authentication: Individual User Accounts, it will create a new project with an already existing IdentityDbContext etc.

Am I meant to continue using this along with my own DbContext? One context for my projects entities, and the other context for the Identity entities? Does this mean I'll have two separate databases, or can I give them both the same connection string?

I know this may be an open ended question, but are there any good resources/tutorials for AspNet Identity?

So far I've only been finding resources about AspNet Identity itself, and not how to integrate it with the rest of the project/database


回答1:


You can specify the same connection string for both of your custom models context and identity context, all you have to do is to change the connection string in the constructor of the ApplicationDbContext class that resides in IdentityModels.cs file like so:

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

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

and the tables needed for identity will be created in the same database as your other entities, as for resource there is a good set of articles on identity here.



来源:https://stackoverflow.com/questions/32326418/use-the-same-database-for-both-my-custom-entities-and-identity-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!