加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 手动脚本ASP.Net标识现有数据库

发布时间:2020-12-15 20:59:57 所属栏目:百科 来源:网络整理
导读:我有一个已经有角色表的现有数据库,我试图让Identity使用表中的现有角色,但我一直收到这个错误. The entity types ‘ApplicationRole’ and ‘Role’ cannot share table ‘Roles’ because they are not in the same type hierarchy or do not have a valid
我有一个已经有角色表的现有数据库,我试图让Identity使用表中的现有角色,但我一直收到这个错误.

The entity types ‘ApplicationRole’ and ‘Role’ cannot share table
‘Roles’ because they are not in the same type hierarchy or do not have
a valid one to one foreign key relationship with matching primary keys
between them.

我想要实现的表映射是,

> AspNetUsers – >用户(ApplicationUser)
> AspNetRoles – >角色(ApplicationRole)
> AspNetUserLogins – > UserLogins(ApplicationUserLogin)
> AspNetUserRoles – > RoleUser(ApplicationUserRole)

由于数据库正被其他应用程序使用,我无法使用代码优先.所以我必须将我的脚本更改发送给DBA.

**现有的SQL表格**

/* Object:  Table [dbo].[Role] */
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Role](
    [ID] [int] IDENTITY(1,1) NOT NULL,[Name] [varchar](50) NOT NULL,[Description] [varchar](100) NOT NULL CONSTRAINT [DF_Role_Description]  DEFAULT (''),CONSTRAINT [PK_Role] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

/* Object:  Table [dbo].[RoleUser] */
CREATE TABLE [dbo].[RoleUser](
    [UserID] [int] NOT NULL,[RoleID] [int] NOT NULL,CONSTRAINT [PK_AppUserRole] PRIMARY KEY CLUSTERED 
(
    [UserID] ASC,[RoleID] ASC
)WITH (PAD_INDEX = OFF,ALLOW_PAGE_LOCKS = ON,FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]

GO

/* Object:  Table [dbo].[User] */
CREATE TABLE [dbo].[User](
    [ID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,[UserName] [dbo].[shortString] NOT NULL CONSTRAINT [DF_User_UserName]  DEFAULT (''),[FirstName] [dbo].[shortString] NULL CONSTRAINT [DF_User_FirstName]  DEFAULT (''),[LastName] [dbo].[shortString] NULL CONSTRAINT [DF_User_LastName]  DEFAULT (''),[Email] [dbo].[longString] NULL CONSTRAINT [DF_User_Email]  DEFAULT (''),[Pager] [dbo].[smallString] NULL CONSTRAINT [DF_User_Pager]  DEFAULT (''),[IsActive] [bit] NOT NULL CONSTRAINT [DF_User_IsActive]  DEFAULT ((1)),[LastPasswordChange] [datetime] NULL,[AccountLockedDate] [datetime] NULL,[AccountLockedByComputerName] [dbo].[shortString] NULL,[AccountLockedByUserName] [dbo].[shortString] NULL,[LastActive] [datetime] NULL,[PasswordHash] [nvarchar](max) NULL,[SecurityStamp] [nvarchar](max) NULL,[Discriminator] [nvarchar](max) NULL,[EmailConfirmed] [bit] NULL,[PhoneNumber] [nvarchar](50) NULL,[PhoneNumberConfirmed] [bit] NULL,[TwoFactorEnabled] [bit] NULL,[LockoutEndDateUtc] [datetime] NULL,[LockoutEnabled] [bit] NULL,[AccessFailedCount] [int] NULL,CONSTRAINT [PK_AppUser] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF,FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

数据库背景

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // Asp.net Identity
            modelBuilder.Entity<ApplicationUser>().ToTable("User");
            modelBuilder.Entity<ApplicationRole>().ToTable("Role");
            modelBuilder.Entity<ApplicationUserClaim>().ToTable("UserClaims");
            modelBuilder.Entity<ApplicationUserLogin>().ToTable("UserLogins");
            modelBuilder.Entity<ApplicationUserRole>().ToTable("RoleUser");
        }

身份课程

public class ApplicationUserLogin : IdentityUserLogin<int> { }
    public class ApplicationUserClaim : IdentityUserClaim<int> { }
    public class ApplicationUserRole : IdentityUserRole<int> { }

    public class ApplicationRole : IdentityRole<int,ApplicationUserRole>,IRole<int>
    {
        public string Description { get; set; }

        public ApplicationRole() : base() { }
        public ApplicationRole(string name)
            : this()
        {
            this.Name = name;
        }

        public ApplicationRole(string name,string description)
            : this(name)
        {
            this.Description = description;
        }
    }

    public class ApplicationUser : IdentityUser<int,ApplicationUserLogin,ApplicationUserRole,ApplicationUserClaim>,IUser<int>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Password { get; set; }

        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser,int> manager)
        {
            var userIdentity = await manager
                .CreateIdentityAsync(this,DefaultAuthenticationTypes.ApplicationCookie);
            return userIdentity;
        }
    }

试图建立FK / PK关系,但没有运气.

解决方法

如果我创建一个新的ASP.NET MVC应用程序并更改您指定的标识内容并创建一个仅包含您指定的表的数据库,我尝试登录然后出现错误

Invalid column name ‘Password’.

这是有道理的,因为表的定义中没有“密码”字段,但该字段是在您的实体中定义的.

然后我从以下位置更改OnModelCreating方法:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    // Asp.net Identity
    modelBuilder.Entity<ApplicationUser>().ToTable("User");
    modelBuilder.Entity<ApplicationRole>().ToTable("Role");
    modelBuilder.Entity<ApplicationUserClaim>().ToTable("UserClaims");
    modelBuilder.Entity<ApplicationUserLogin>().ToTable("UserLogins");
    modelBuilder.Entity<ApplicationUserRole>().ToTable("RoleUser");
}

至:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    // Asp.net Identity
    modelBuilder.Entity<ApplicationUser>().ToTable("User");
    modelBuilder.Entity<ApplicationRole>().ToTable("Role");
    modelBuilder.Entity<ApplicationUserClaim>().ToTable("Role");
    modelBuilder.Entity<ApplicationUserLogin>().ToTable("UserLogins");
    modelBuilder.Entity<ApplicationUserRole>().ToTable("RoleUser");
}

然后我得到错误:

The entity types ‘ApplicationUserClaim’ and ApplicationRole’ cannot share table ‘Role‘ because they are not in the same type hierarchy or do not have a valid one to one foreign key relationship with matching primary keys between them.

注意我的错误如何抱怨表Role,而你的错误是抱怨表Roles

所以,考虑到所有这些,我有一些观察,建议和要求:

>没有任何根本问题阻止您以您想要的方式使用ASP.NET身份,因为我能够在全新安装时根据您的要求进行设置并超越它抱怨实体类型共享表的地步.
>也许您的应用中还有其他实体存在冲突,可能吗?
>您的数据库中是否包含角色和角色表?或者项目中的实体?你确定这是你得到的错误,使用你发布的确切代码吗?就像我说的那样,奇怪的是你错误地抱怨Roles和我的角色
>您是否能够提供问题的最小可验证示例(https://stackoverflow.com/help/mcve),正如我所说,我无法从干净的MVC项目中重现问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读