【发布时间】:2016-09-16 17:25:55
【问题描述】:
我有一个现有的数据库,其中已有一个角色表,我正在尝试让 Identity 使用表中的现有角色,但我不断收到此错误。
实体类型“ApplicationRole”和“Role”不能共享表 “角色”,因为它们不在同一类型层次结构中或没有 具有匹配主键的有效一对一外键关系 他们之间。
我想要实现的表映射是,
- AspNetUsers -> 用户 (ApplicationUser)
- AspNetRoles -> 角色 (ApplicationRole)
- AspNetUserLogins -> 用户登录 (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, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 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, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, 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 关系但没有成功。
【问题讨论】:
-
你真正想做什么?您是否需要 ApplicationRole 和 Role 来共享相同的现有表?
-
ApplicationRole 只是扩展了我的 IdentityRole 以使用 int 而不是字符串。我只是想让 Identity 使用 Role 表来管理角色。
标签: c# asp.net asp.net-identity