ASP.NET MVC5 - 将用户保留在 Oracle 数据库中 [英] ASP.NET MVC5 - Keeping Users in Oracle Database

查看:39
本文介绍了ASP.NET MVC5 - 将用户保留在 Oracle 数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建 ASP.NET MVC5 项目后(目标框架为 .NET 4.5.1,身份验证类型为个人用户帐户),那么配置项目以将用户、声明、角色等保存在 Oracle 12c 数据库中的最优雅的方法是什么?我的意思是,如何在不破坏自动生成的 MVC5 项目结构的情况下将授权/身份验证数据保留在 Oracle 中.

Once creating a ASP.NET MVC5 project (with target framework is .NET 4.5.1 and the authentication type is Individual User Account), so what is the most elegant way to configure the project so that it keeps the user, claims, roles etc. in an Oracle 12c database? I mean, how can I keep the authorization/authentication data in Oracle without deforming the automatically-generated MVC5 project structure.

我想改变 <defaultConnection> 标记是不够的,应该有另一个 Oracle 实现来替换 Microsoft.AspNet.Identity.EntityFramework.从一开始就收集一个告诉你要做什么的答案会非常有帮助,模板项目是由 VisualStudio 生成的(即应该将哪些引用添加到项目中;应该以哪种方式添加 Web.config 文件)安排好了吗?Oracle 表应该严格命名为 AspNetUsers、AspNetClaims 等,还是可以将已经存在的表名注入代码端?)

I guess changing the <defaultConnection> tag is not enough and there should be another Oracle implementation to replace Microsoft.AspNet.Identity.EntityFramework. It would be very helpful to gather an answer telling what to do step by step right from the start, where the template project is generated by VisualStudio (i.e. which references should be added to the project; in which way the Web.config file should be arranged? Should the Oracle tables be named strictly as AspNetUsers, AspNetClaims etc. or can I inject the already existing table names into the code side?)

注意: Devart 的 dotConnect for Oracle 不在我的范围内,因为它不是免费产品.我使用 Oracle ManagedDataAccess 进行数据库访问并使用 Entity Framework,但是使用 Oracle 数据库的 ASP.NET Identity 2 + EntityFramework6 逻辑(没有深刻改变经典 MVC5 项目的结构)已经令我沮丧.

Note: Devart's dotConnect for Oracle is out of my scope as it is not a free product. I use Oracle ManagedDataAccess for database access and to make use of Entity Framework, but using the ASP.NET Identity 2 + EntityFramework6 logic with Oracle database (without deeply changing the structure of the classical MVC5 project) has been frustrating to me.

推荐答案

这对你来说可能有点晚了,但我会留下它,以防其他人遇到同样的问题.所以我终于设法让 Identity 2.0 和 Oracle 一起工作.如果您不想对默认 IdentityUser 进行任何更改(例如,如果您可以使用 char ID 而不是 int 或 long)并且只想要现有 Oracle 架构上的表,则以下步骤有效.

This may arrive a bit late for you but I'll leave it in case anyone else runs into the same problem. So I finally managed to make Identity 2.0 and Oracle work together. The following steps work if you don't want to make any changes to the default IdentityUser (ex. if you're ok with having a char ID instead of int or long) and just want the tables on your existing Oracle schema.

  1. 在 Oracle 上创建身份表.如果需要,您可以更改表名,只需确保包含 Identity 使用它所需的列.您还可以在应用程序中添加您可能需要的任何额外列(最初在 Devart,我将它复制到一个要点以防 URL 中断):

  1. Create Identity tables on Oracle. You can change the table names if you want to, just make sure to include the necessary columns for Identity to work with it. You can also add any extra columns you may need on your application (script originally found on Devart, I copied it to a gist in case URL breaks):

要点这里

如果您使用的是 EDMX 文件,则需要添加一个新的连接字符串,因为自动生成的连接字符串不起作用,您需要一个标准连接字符串.尝试遵循此模板:

If you're using an EDMX file, you need to add a new connection string cause the one that gets generated automatically won't work, you need a standard connection string. Try following this template:

告诉您的 ApplicationDbContext 使用您的新 connectionString

Tell your ApplicationDbContext to use your new connectionString

public ApplicationDbContext()
    : base("IdentityContext", throwIfV1Schema: false)
{
}

  • 告诉 Identity 使用您现有的架构和表.在 IdentityModels.cs 中的 ApplicationDbContext 定义中添加此方法:

  • Tell Identity to use your existing schema and tables. Add this method inside the ApplicationDbContext definition found in IdentityModels.cs:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder); // MUST go first.
    
        modelBuilder.HasDefaultSchema("YOUR_SCHEMA"); // Use uppercase!
    
        modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers");
        modelBuilder.Entity<IdentityRole>().ToTable("AspNetRoles");
        modelBuilder.Entity<IdentityUserRole>().ToTable("AspNetUserRoles");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("AspNetUserLogins");
    }
    

  • 重建就是这样!

  • Rebuild and thats it!

    让我知道它是否适合你!

    Let me know if it works for you!

    这篇关于ASP.NET MVC5 - 将用户保留在 Oracle 数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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