asp.net-mvc - 您自定义数据库IdentityDbContext

标签 asp.net-mvc entity-framework authentication

我试图开发一个应用程序并创建了自己的数据库,然后首先使用数据库将其反射(reflect)到实体模型中。所以我现在已经有了 myOwnDBContext。 然后我认为将它与 IdentityDbContext 生成的类混合是一个好主意。但是当我像这样更改 IdentityModel 中的连接字符串时

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("myOwnDBConnectionString")
        {
        }
    }

这些表没有按照我预期在 myOwnDB 中生成,而是收到以下错误。

The entity type ApplicationUser is not part of the model for the current context.

有没有办法可以将 IdentityDbContext 的默认数据库与我自己的数据库混合?

最佳答案

我不确定这是否可以在不使用 EF 迁移的情况下工作,但我确实可以使用迁移。具体来说,有两个 DbContext 派生类(一个用于自定义实体,一个从 IdentityDbContext 派生)。

对于迁移,我必须从包管理器控制台为两个上下文启用迁移,并使用 -MigrationsDirectory 参数将两组迁移放入不同的文件夹中(EF6 的一项新功能)。

然后,当运行 update-database 时,会为每个上下文正确创建表。 update-database 命令需要 -ConfigurationTypeName 参数来指定要使用的确切迁移。

关于asp.net-mvc - 您自定义数据库IdentityDbContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19784233/

相关文章:

asp.net-mvc - 依赖注入(inject)框架有什么意义?

c# - 扩展模型以添加方法

authentication - Maven 服务器身份验证作为配置文件属性

c# - 如何在我的 ASP.net Core 应用程序中同时使用 Bearer 和 Cookie 身份验证?

Java - Spring - 基本身份验证 - 如何对 application.properties 中定义的密码进行哈希处理

c# - asp.net core 3 MVC 上的模型绑定(bind)属性为 null

c# - .Net Core MVC 1.0 中的 DI

asp.net-mvc - 如何确定操作方法是否具有 HttpPost 属性?

entity-framework - EF 核心 : Built in Created and Edited timestamps

c# - 'Microsoft.EntityFrameworkCore.Infrastructure.IDbContextFactory`1[TContext ]' violates the constraint of type parameter ' TContext'