首页 文章

ASP.NET MVC 5在Oracle数据库中存储标识用户

提问于
浏览
3

我是开发ASP.NET应用程序的新手,我开始了一个新项目,我正在使用带有ODP和OLAC插件的Oracle DB . 我设置了连接字符串,我可以在Server Explorer下看到数据库 .

现在我希望Identity用户也可以存储在我的数据库中,但我现在无法找到它们存储的位置 . app_data文件夹为空,任何地方都没有.mdf文件,我的连接字符串是Oracle连接字符串;但是当我设置IdentityDBContext以使用我自己的连接字符串时,我收到错误说 "The entity type ApplicationUser is not part of the model for the current context." 我正在使用数据库优先方法:

这是我的数据库上下文的自动生成代码:

public partial class ActivoContext : DbContext
{
    public ActivoContext()
        : base("name=ActivoContext")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }
......

这是项目生成的身份代码,我刚刚更改了连接字符串:

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

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

web.config文件:

<connectionStrings>
  <add name="ActivoContext" connectionString="metadata=res://*/Activo.csdl|res://*/Activo.ssdl|res://*/Activo.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string=&quot;DATA SOURCE=localhost:1521/xe;PASSWORD=activos;USER ID=ACTIVOS&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

EDIT 所以我意识到问题是使用相同的连接字符串有两个不同的上下文 . 因此,只有一个显示而另一个不起作用:自动生成的db-first ActivoContext显示而ApplicationDbContext不显示 . 我该如何合并这两个或让它们一起工作?我无法修改DbContext导致其自动生成...

2 回答

  • 7

    最后让Identity 2.0与我的Oracle DB一起使用 . 这是我做的:

    如果您不想对默认的IdentityUser进行任何更改,则以下步骤有效(例如,如果您可以使用char ID而不是int或long),并且只需要现有Oracle架构上的表 .

    1)在Oracle上创建标识表 . 您可以根据需要更改表名,只需确保包含必要的Identity列以使用它 . 您还可以在应用程序中添加可能需要的任何额外列(最初在Devart上找到的脚本,我将其复制到gist,以防URL中断):

    要点here

    2)如果您正在使用EDMX文件,则需要添加新的连接字符串,因为自动生成的连接字符串将无法工作,您需要一个标准的连接字符串 . 请尝试以下模板:

    <add name="IdentityContext" connectionString="Data Source=localhost:1521/xe;PASSWORD=password;USER ID=username;" providerName="Oracle.ManagedDataAccess.Client" />
    

    3)告诉您的ApplicationDbContext使用新的connectionString

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

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

    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");
    }
    

    5)重建,就是这样!

  • -1

    您可以使用此脚本在Oracle DB中正确创建标识表

    `

    CREATE TABLE incapp.AspNetRoles (   
      Id VARCHAR2(128) NOT NULL,  
      Name VARCHAR2(256) NOT NULL  
     ) pctfree 20 pctused 70 tablespace INCTBL;  
     create unique index incapp.pk_aspnetroles on incapp.AspNetRoles (  
      id  
     ) pctfree 10 tablespace incidx;  
     alter table incapp.AspNetRoles add (  
      constraint pk_aspnetroles  
      primary key (id)  
     );  
     create public synonym AspNetRoles for incapp.AspNetRoles;  
     grant select,insert,update,delete on aspnetroles to webapps;  
     CREATE TABLE incapp.AspNetUserRoles (   
      UserId VARCHAR2(128) NOT NULL,  
      RoleId VARCHAR2(128) NOT NULL  
     ) pctfree 20 pctused 70 tablespace INCTBL;  
     create unique index incapp.pk_AspNetUserRoles on incapp.AspNetUserRoles (  
      UserId, RoleId  
     ) pctfree 10 tablespace incidx;  
     alter table incapp.AspNetUserRoles add (  
      constraint pk_AspNetUserRoles  
      primary key (UserId, RoleId)  
     );  
     create public synonym AspNetUserRoles for incapp.AspNetUserRoles;  
     grant select,insert,update,delete on incapp.AspNetUserRoles to webapps;  
     CREATE TABLE incapp.AspNetUsers (   
      Id VARCHAR2(128) NOT NULL,  
      Email VARCHAR2(256) NULL,  
      EmailConfirmed NUMBER(1) NOT NULL,  
      PasswordHash VARCHAR2(256) NULL,  
      SecurityStamp VARCHAR2(256) NULL,  
      PhoneNumber VARCHAR2(256) NULL,  
      PhoneNumberConfirmed NUMBER(1) NOT NULL,  
      TwoFactorEnabled NUMBER(1) NOT NULL,  
      LockoutEndDateUtc TIMESTAMP(7) NULL,  
      LockoutEnabled NUMBER(1) NOT NULL,  
      AccessFailedCount NUMBER(10) NOT NULL,  
      UserName VARCHAR2(256) NOT NULL  
     ) pctfree 20 pctused 70 tablespace INCTBL;  
     create unique index incapp.pk_AspNetUsers on incapp.AspNetUsers (  
      id  
     ) pctfree 10 tablespace incidx;  
     alter table incapp.AspNetUsers add (  
      constraint pk_AspNetUsers  
      primary key (id)  
     );  
     create public synonym AspNetUsers for incapp.AspNetUsers;  
     grant select,insert,update,delete on incapp.AspNetUsers to webapps;  
     CREATE TABLE AspNetUserClaims (   
      Id NUMBER(10) NOT NULL,  
      UserId VARCHAR2(128) NOT NULL,  
      ClaimType VARCHAR2(256) NULL,  
      ClaimValue VARCHAR2(256) NULL  
     ) pctfree 20 pctused 70 tablespace INCTBL;  
     create unique index incapp.pk_AspNetUserClaims on incapp.AspNetUserClaims (  
      id  
     ) pctfree 10 tablespace incidx;  
     alter table incapp.AspNetUserClaims add (  
      constraint pk_AspNetUserClaims  
      primary key (id)  
     );  
     create public synonym AspNetUserClaims for incapp.AspNetUserClaims;  
     grant select,insert,update,delete on incapp.AspNetUserClaims to webapps;  
     CREATE SEQUENCE incapp.AspNetUserClaims_SEQ;  
     create public synonym AspNetUserClaims_SEQ for incapp.AspNetUserClaims_SEQ;  
     CREATE OR REPLACE TRIGGER incapp.AspNetUserClaims_INS_TRG  
      BEFORE INSERT ON incapp.AspNetUserClaims  
      FOR EACH ROW  
     BEGIN  
      :NEW.Id := AspNetUserClaims_SEQ.NEXTVAL;  
     END;  
     /  
     CREATE TABLE incapp.AspNetUserLogins (   
      LoginProvider VARCHAR2(128) NOT NULL,  
      ProviderKey VARCHAR2(128) NOT NULL,  
      UserId VARCHAR2(128) NOT NULL  
     ) pctfree 20 pctused 70 tablespace INCTBL;  
     create unique index incapp.pk_AspNetUserLogins on incapp.AspNetUserLogins (  
      LoginProvider, ProviderKey, UserId  
     ) pctfree 10 tablespace incidx;  
     alter table incapp.AspNetUserLogins add (  
      constraint pk_AspNetUserLogins  
      primary key (LoginProvider, ProviderKey, UserId)  
     );  
     create public synonym AspNetUserLogins for incapp.AspNetUserLogins;  
     grant select,insert,update,delete on incapp.AspNetUserLogins to webapps;  
     create unique index incapp.RoleNameIndex on incapp.AspNetRoles (  
      name  
     ) pctfree 10 tablespace incidx;  
     alter table incapp.AspNetRoles add (  
      constraint uq_RoleNameIndex  
      unique (name)  
     );  
     create index incapp.IX_AspNetUserRoles_UserId on incapp.AspNetUserRoles (  
      UserId  
     ) pctfree 10 tablespace incidx;  
     create index incapp.IX_AspNetUserRoles_RoleId on incapp.AspNetUserRoles (  
      RoleId  
     ) pctfree 10 tablespace incidx;  
     create unique index incapp.UserNameIndex on incapp.AspNetUsers (  
      UserName  
     ) pctfree 10 tablespace incidx;  
     create index incapp.IX_AspNetUserClaims_UserId on incapp.AspNetUserClaims (  
      UserId  
     ) pctfree 10 tablespace incidx;  
     create index incapp.IX_AspNetUserLogins_UserId on incapp.AspNetUserLogins (  
      UserId  
     ) pctfree 10 tablespace incidx;  
     ALTER TABLE incapp.AspNetUserRoles  
      ADD CONSTRAINT FK_UserRoles_Roles FOREIGN KEY (RoleId) REFERENCES incapp.AspNetRoles (Id)  
      ON DELETE CASCADE;  
     ALTER TABLE incapp.AspNetUserRoles  
      ADD CONSTRAINT FK_UserRoles_Users FOREIGN KEY (UserId) REFERENCES incapp.AspNetUsers (Id)  
      ON DELETE CASCADE;  
     ALTER TABLE incapp.AspNetUserClaims  
      ADD CONSTRAINT FK_UserClaims_Users FOREIGN KEY (UserId) REFERENCES incapp.AspNetUsers (Id)  
      ON DELETE CASCADE;  
     ALTER TABLE incapp.AspNetUserLogins  
      ADD CONSTRAINT FK_UserLogins_Users FOREIGN KEY (UserId) REFERENCES incapp.AspNetUsers (Id)  
      ON DELETE CASCADE;
    

    `

    像下面一样修改ConnectionString

    <add name="DBContext" connectionString="Data Source=AAAA:1521/AA;PASSWORD=AAAA;USER ID=AAAA;" providerName="Oracle.ManagedDataAccess.Client" />
    

    然后修改你的OnModelCreating方法

    `

    protected override void OnModelCreating(DbModelBuilder modelBuilder)  
         {  
           base.OnModelCreating(modelBuilder);  
           modelBuilder.HasDefaultSchema("AAAAA");
           modelBuilder.Entity<ApplicationUser>()  
           .ToTable("ASPNETUSERS").Property(p => p.UserName).HasColumnName("USERNAME");  
           modelBuilder.Entity<ApplicationUser>()  
           .ToTable("ASPNETUSERS").Property(p => p.AccessFailedCount).HasColumnName("ACCESSFAILEDCOUNT");  
           modelBuilder.Entity<ApplicationUser>()  
           .ToTable("ASPNETUSERS").Property(p => p.LockoutEnabled).HasColumnName("LOCKOUTENABLED");  
           modelBuilder.Entity<ApplicationUser>()  
           .ToTable("ASPNETUSERS").Property(p => p.LockoutEndDateUtc).HasColumnName("LOCKOUTENDDATEUTC");  
           modelBuilder.Entity<ApplicationUser>()  
           .ToTable("ASPNETUSERS").Property(p => p.TwoFactorEnabled).HasColumnName("TWOFACTORENABLED");  
           modelBuilder.Entity<ApplicationUser>()  
           .ToTable("ASPNETUSERS").Property(p => p.PhoneNumberConfirmed).HasColumnName("PHONENUMBERCONFIRMED");  
           modelBuilder.Entity<ApplicationUser>()  
           .ToTable("ASPNETUSERS").Property(p => p.PhoneNumber).HasColumnName("PHONENUMBER");  
           modelBuilder.Entity<ApplicationUser>()  
           .ToTable("ASPNETUSERS").Property(p => p.SecurityStamp).HasColumnName("SECURITYSTAMP");  
           modelBuilder.Entity<ApplicationUser>()  
           .ToTable("ASPNETUSERS").Property(p => p.PasswordHash).HasColumnName("PASSWORDHASH");  
           modelBuilder.Entity<ApplicationUser>()  
           .ToTable("ASPNETUSERS").Property(p => p.EmailConfirmed).HasColumnName("EMAILCONFIRMED");  
           modelBuilder.Entity<ApplicationUser>()  
           .ToTable("ASPNETUSERS").Property(p => p.Email).HasColumnName("EMAIL");  
           modelBuilder.Entity<ApplicationUser>()  
           .ToTable("ASPNETUSERS").Property(p => p.Id).HasColumnName("ID");  
           modelBuilder.Entity<IdentityUserRole>()  
           .ToTable("ASPNETUSERROLES").Property(p => p.RoleId).HasColumnName("ROLEID");  
           modelBuilder.Entity<IdentityUserRole>()  
           .ToTable("ASPNETUSERROLES").Property(p => p.UserId).HasColumnName("USERID");  
           modelBuilder.Entity<IdentityUserLogin>()  
           .ToTable("ASPNETUSERLOGINS").Property(p => p.UserId).HasColumnName("USERID");  
           modelBuilder.Entity<IdentityUserLogin>()  
           .ToTable("ASPNETUSERLOGINS").Property(p => p.ProviderKey).HasColumnName("PROVIDERKEY");  
           modelBuilder.Entity<IdentityUserLogin>()  
           .ToTable("ASPNETUSERLOGINS").Property(p => p.LoginProvider).HasColumnName("LOGINPROVIDER");  
           modelBuilder.Entity<IdentityUserClaim>()  
           .ToTable("ASPNETUSERCLAIMS").Property(p => p.Id).HasColumnName("ID");  
           modelBuilder.Entity<IdentityUserClaim>()  
           .ToTable("ASPNETUSERCLAIMS").Property(p => p.UserId).HasColumnName("USERID");  
           modelBuilder.Entity<IdentityUserClaim>()  
           .ToTable("ASPNETUSERCLAIMS").Property(p => p.ClaimType).HasColumnName("CLAIMTYPE");  
           modelBuilder.Entity<IdentityUserClaim>()  
           .ToTable("ASPNETUSERCLAIMS").Property(p => p.ClaimValue).HasColumnName("CLAIMVALUE");  
           modelBuilder.Entity<IdentityRole>()  
           .ToTable("ASPNETROLES").Property(p => p.Id).HasColumnName("ID");  
           modelBuilder.Entity<IdentityRole>()  
           .ToTable("ASPNETROLES").Property(p => p.Name).HasColumnName("NAME");  
         }
    

    `

    这对我的项目工作得很好 .

    你可以在这里找到一些有趣的点 . (ASP.NET MVC,EF与NgGet Oracle 12.2.1 ..)

    ASP.NET Identity With Oracle Database

相关问题