扩展 Identity 2 中 IdentityUserRole 的主键
extending PrimaryKey of IdentityUserRole in Identity 2
是否可以通过附加列扩展 ApplicationUserRole 的主键?
默认有两列定义为主键:UserId
和RoleId
这里是默认实现
public partial class ApplicationUserRole : IdentityUserRole<string>
{
public virtual ApplicationUser User { get; set; }
public virtual ApplicationRole Role { get; set; }
}
public class IdentityUserRole<TKey>
{
public IdentityUserRole();
// Summary:
// RoleId for the role
public virtual TKey RoleId { get; set; }
//
// Summary:
// UserId for the user that is in the role
public virtual TKey UserId { get; set; }
}
数据库中的 table 看起来像
CREATE TABLE [dbo].[AspNetUserRoles](
[UserId] [nvarchar](128) NOT NULL,
[RoleId] [nvarchar](128) NOT NULL,
CONSTRAINT [PK_dbo.AspNetUserRoles] 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) ON [PRIMARY]
) ON [PRIMARY]
但我需要更多! :)
我的意思是我希望我的模型看起来像
public partial class ApplicationUserRole : IdentityUserRole<string>
{
public Guid ServiceId { get; set; }
public virtual ApplicationUser User { get; set; }
public virtual ApplicationRole Role { get; set; }
public virtual Service Service { get; set; }
}
我不明白如何用数据注释或流利的方式正确地做到这一点api。我都试过了,但都没有成功。
迁移引擎似乎忽略了该模型的任何主键更改。
至少当我添加注释或流利的 api 方法时,迁移引擎会生成空的迁移脚本。
我刚刚添加了 serviceId 属性 并且我的 table 现在看起来像
CREATE TABLE [dbo].[AspNetUserRoles](
[UserId] [nvarchar](128) NOT NULL,
[RoleId] [nvarchar](128) NOT NULL,
[ServiceId] [uniqueidentifier] NOT NULL,
CONSTRAINT [PK_dbo.AspNetUserRoles] 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) ON [PRIMARY]
) ON [PRIMARY]
当我尝试通过注释或流利应用关键更改时 api
迁移脚本看起来像
public partial class rolesmigrate02 : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
流利api:
modelBuilder.Entity<ApplicationUserRole>().HasKey(hk => new { hk.UserId, hk.RoleId, hk.ServiceId});
注释
public partial class ApplicationUserRole : IdentityUserRole<string>
{
[Key, Column(Order = 0)]
public override string UserId { get; set; }
[Key, Column(Order = 1)]
public override string RoleId { get; set; }
[Key, Column(Order = 2)]
public Guid ServiceId { get; set; }
public virtual ApplicationUser User { get; set; }
public virtual ApplicationRole Role { get; set; }
public virtual Service Service { get; set; }
}
我可以在数据库中手动进行更改,但我怀疑
我的错误在哪里?为什么我不能通过代码优先迁移来扩展这个模型?
花了一些时间后我发现了这个postSam about identity 2 on Whosebug
关键在base.OnModelCreating(modelBuilder)
如果我们想重新应用或修改预定义的身份 2 映射,我们应该在 base.OnModelCreating.
之后进行
这么流利api应该是
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// usualy i put this line to the end of this method but it should be before
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUserRole>().HasKey(hk => new { hk.UserId, hk.RoleId, hk.ServiceId});
}
是否可以通过附加列扩展 ApplicationUserRole 的主键?
默认有两列定义为主键:UserId
和RoleId
这里是默认实现
public partial class ApplicationUserRole : IdentityUserRole<string>
{
public virtual ApplicationUser User { get; set; }
public virtual ApplicationRole Role { get; set; }
}
public class IdentityUserRole<TKey>
{
public IdentityUserRole();
// Summary:
// RoleId for the role
public virtual TKey RoleId { get; set; }
//
// Summary:
// UserId for the user that is in the role
public virtual TKey UserId { get; set; }
}
数据库中的 table 看起来像
CREATE TABLE [dbo].[AspNetUserRoles](
[UserId] [nvarchar](128) NOT NULL,
[RoleId] [nvarchar](128) NOT NULL,
CONSTRAINT [PK_dbo.AspNetUserRoles] 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) ON [PRIMARY]
) ON [PRIMARY]
但我需要更多! :) 我的意思是我希望我的模型看起来像
public partial class ApplicationUserRole : IdentityUserRole<string>
{
public Guid ServiceId { get; set; }
public virtual ApplicationUser User { get; set; }
public virtual ApplicationRole Role { get; set; }
public virtual Service Service { get; set; }
}
我不明白如何用数据注释或流利的方式正确地做到这一点api。我都试过了,但都没有成功。 迁移引擎似乎忽略了该模型的任何主键更改。 至少当我添加注释或流利的 api 方法时,迁移引擎会生成空的迁移脚本。
我刚刚添加了 serviceId 属性 并且我的 table 现在看起来像
CREATE TABLE [dbo].[AspNetUserRoles](
[UserId] [nvarchar](128) NOT NULL,
[RoleId] [nvarchar](128) NOT NULL,
[ServiceId] [uniqueidentifier] NOT NULL,
CONSTRAINT [PK_dbo.AspNetUserRoles] 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) ON [PRIMARY]
) ON [PRIMARY]
当我尝试通过注释或流利应用关键更改时 api 迁移脚本看起来像
public partial class rolesmigrate02 : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
流利api:
modelBuilder.Entity<ApplicationUserRole>().HasKey(hk => new { hk.UserId, hk.RoleId, hk.ServiceId});
注释
public partial class ApplicationUserRole : IdentityUserRole<string>
{
[Key, Column(Order = 0)]
public override string UserId { get; set; }
[Key, Column(Order = 1)]
public override string RoleId { get; set; }
[Key, Column(Order = 2)]
public Guid ServiceId { get; set; }
public virtual ApplicationUser User { get; set; }
public virtual ApplicationRole Role { get; set; }
public virtual Service Service { get; set; }
}
我可以在数据库中手动进行更改,但我怀疑
我的错误在哪里?为什么我不能通过代码优先迁移来扩展这个模型?
花了一些时间后我发现了这个postSam about identity 2 on Whosebug
关键在base.OnModelCreating(modelBuilder)
如果我们想重新应用或修改预定义的身份 2 映射,我们应该在 base.OnModelCreating.
之后进行这么流利api应该是
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// usualy i put this line to the end of this method but it should be before
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUserRole>().HasKey(hk => new { hk.UserId, hk.RoleId, hk.ServiceId});
}