映射枚举列表
我有一个名为 UserPermissions 的表,其中通过 userId 与用户表进行 FK,然后是一个用于枚举字符串值的字符串列。
我看到的错误是 NHibernate.MappingException:表 UserPermissions 中的关联引用了未映射的类:GotRoleplay.Core.Domain.Model.Permission
我的权限枚举:
public enum Permission
{
[StringValue("Add User")]
AddUser,
[StringValue("Edit User")]
EditUser,
[StringValue("Delete User")]
DeleteUser,
[StringValue("Add Content")]
AddContent,
[StringValue("Edit Content")]
EditContent,
[StringValue("Delete Content")]
DeleteContent,
}
我的用户类中的属性:
public virtual IList<Permission> Permissions { get; set; }
我的数据库表:
CREATE TABLE dbo.UserPermissions
(
UserPermissionId int IDENTITY(1,1) NOT NULL,
UserId int NOT NULL,
PermissionName varchar (50) NOT NULL,
CONSTRAINT PK_UserPermissions PRIMARY KEY CLUSTERED (UserPermissionId),
CONSTRAINT FK_UserPermissions_Users FOREIGN KEY (UserId) REFERENCES Users(UserId),
CONSTRAINT U_User_Permission UNIQUE(UserId, PermissionName)
)
我的映射尝试我的用户对象的权限属性:
HasManyToMany(x => x.Permissions)
.WithParentKeyColumn("UserId")
.WithChildKeyColumn("PermissionName")
.WithTableName("UserPermissions")
.LazyLoad();
我做错了什么,它无法将权限映射到枚举值列表?
I have a table called UserPermissions with a FK to the users table by userId and then a string column for the string value of an enum.
The error I am seeing is NHibernate.MappingException: An association from the table UserPermissions refers to an unmapped class: GotRoleplay.Core.Domain.Model.Permission
My Permission Enum:
public enum Permission
{
[StringValue("Add User")]
AddUser,
[StringValue("Edit User")]
EditUser,
[StringValue("Delete User")]
DeleteUser,
[StringValue("Add Content")]
AddContent,
[StringValue("Edit Content")]
EditContent,
[StringValue("Delete Content")]
DeleteContent,
}
The property in my User class:
public virtual IList<Permission> Permissions { get; set; }
My database table:
CREATE TABLE dbo.UserPermissions
(
UserPermissionId int IDENTITY(1,1) NOT NULL,
UserId int NOT NULL,
PermissionName varchar (50) NOT NULL,
CONSTRAINT PK_UserPermissions PRIMARY KEY CLUSTERED (UserPermissionId),
CONSTRAINT FK_UserPermissions_Users FOREIGN KEY (UserId) REFERENCES Users(UserId),
CONSTRAINT U_User_Permission UNIQUE(UserId, PermissionName)
)
My attempt at mapping the permissions property of my user object:
HasManyToMany(x => x.Permissions)
.WithParentKeyColumn("UserId")
.WithChildKeyColumn("PermissionName")
.WithTableName("UserPermissions")
.LazyLoad();
What am I doing wrong that it can't map the permission to a list of enum values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是对我有用的方法,
请在此处查看更多信息答案
here is the way which worked for me
look here for more information answer
您需要指定类型,以便 NHibernate 可以将表中的值转换为 Permission 枚举的成员。
编辑添加:
抱歉,我应该注意到您将其设置为ManyToMany。 这是不可能的:您不能将 Users 集合(m:m 的另一侧)挂在枚举上。 您需要将其定义为 1:m 或创建一个权限表和类并将其映射为 m:m。
You need to specify the type so that NHibernate can convert the value in the table to a member of the Permission enum.
Edited to add:
I'm sorry, I should have noticed that you had this as ManyToMany. That's not possible: You can't have a Users collection (other side of m:m) hanging off an enum. You need to define this as 1:m or create a Permission table and class and map that as m:m.
我想我会发布我为解决方案选择的代码。 这只是一个解决方法。 我希望 Fluent 能够支持枚举列表,但在它支持之前,这里有一个可能的解决方案:
枚举 - 这是我的枚举,你的标准枚举。
接下来,我有我的许可课程。
如您所见,我有一个 ID 和一个名称,还有一个将 Id 转换为 PermissionCode 枚举的属性。
映射如下所示:
由于 PermissionCode 属性是派生的,因此我们在映射中不执行任何操作。
映射背后的我的表结构如下所示:
如果您想使用名称而不是整数值,只需稍加修改即可。 这取决于您的个人喜好。
I thought I would post the code that I chose for my solution. It's only a workaround. I wish Fluent would support Enum lists, but until it does, here's a possible solution:
The Enum - This is my enum, your standard enum.
Next, I have my Permission class.
As you can see, I have an ID and a name, and then a property that converts the Id into my PermissionCode enum.
The mapping looks like this:
Since the PermissionCode property is derived, we don't do anything in the mapping.
My Table structure behind the mapping looks like this:
If you wanted to use the name instead of the integer value, with some slight modifications you can. It depends on your personal preferences.