我的自定义实体和身份上下文都使用同一个数据库? [英] Use the same database for both my custom entities and identity context?

查看:70
本文介绍了我的自定义实体和身份上下文都使用同一个数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用用户登录/身份验证等方式创建MVC网站.

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

我正在使用EF CodeFirst创建数据库.

I'm using EF CodeFirst to create my database.

如果我创建一个新的MVC项目并选择身份验证:单个用户帐户",它将创建一个具有现有IdentityIbContext等的新项目.

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.

我是否打算继续将其与我自己的DbContext一起使用?我的项目实体的一个上下文,身份实体的另一个上下文?这是否意味着我将拥有两个单独的数据库,或者我可以给它们两个提供相同的连接字符串?

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?

我知道这可能是一个开放式的问题,但是对于AspNet Identity是否有任何好的资源/教程?

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

到目前为止,我只找到有关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

推荐答案

您可以为自定义模型上下文和标识上下文指定相同的连接字符串,所有要做的就是在以下构造函数中更改连接字符串:像这样位于IdentityModels.cs文件中的ApplicationDbContext类:

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.

这篇关于我的自定义实体和身份上下文都使用同一个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆