如何在Lightswitch的SecurityData数据工作空间中重命名角色的名称?
//Lookup the role, and rename it, or create one if none exist.
var rolesQuery = this.DataWorkspace.SecurityData.Roles.Where(r => r.Name.StartsWith(oldRoleNameString)).Execute();
Role role = rolesQuery.Any() ? rolesQuery.Single() : this.DataWorkspace.SecurityData.Roles.AddNew();
role.Name = "OtherName";
抛出InvalidOperationException,指出“无法设置属性'Name'的值,因为它是只读的。”
那么这样做的正确方法是什么?
答案 0 :(得分:0)
无法重命名角色。这有几个原因: 1. LightSwitch将角色实现基于ASP.NET的Membership / Role系统,该系统不提供重命名角色的API。 2.无法更新Name属性,因为它是主键,LightSwitch不允许更改主键的值。
相反,您需要删除该角色并添加一个新角色来替换它。
答案 1 :(得分:0)
这是asp.net角色中的一个错误,即使在底层数据模型中,表asp_net_Roles使用GUID列作为主键,也会引用名称引用的角色:
/****** Object: Table [dbo].[aspnet_Roles] Script Date: 26.01.2015 13:56:15 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[aspnet_Roles](
[ApplicationId] [uniqueidentifier] NOT NULL,
[RoleId] [uniqueidentifier] NOT NULL DEFAULT (newid()),
[RoleName] [nvarchar](256) NOT NULL,
[LoweredRoleName] [nvarchar](256) NOT NULL,
[Description] [nvarchar](256) NULL,
CONSTRAINT [PK_aspnet_Roles_RoleId] PRIMARY KEY NONCLUSTERED
(
[RoleId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 95) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[aspnet_Roles] WITH CHECK ADD CONSTRAINT [FK_aspnet_Roles_ApplicationId_aspnet_Applications_ApplicationId] FOREIGN KEY([ApplicationId])
REFERENCES [dbo].[aspnet_Applications] ([ApplicationId])
GO
ALTER TABLE [dbo].[aspnet_Roles] CHECK CONSTRAINT [FK_aspnet_Roles_ApplicationId_aspnet_Applications_ApplicationId]
GO
太糟糕了!