无法通过 NHibernate/ActiveRecord 将字段保留到 Aspnet_Users
我在后端使用 ActiveRecord 和 NHibernate。我已经为用户设置了映射;我可以毫无问题地创建/检索/注册用户。
我现在想要向我的用户添加一个名为 Role
的关联(每个角色有许多用户)。我已经创建了适当的 Role
类、表、数据等,并且一切似乎都在为此目的工作。
问题是,当我保存用户并关联角色时,该关联不会保留到数据库中。
我已将 RoleId (int16)
列添加到 aspnet_Users
表中,以匹配 Role 表的 Id (int16)
列。我尝试使用 Save
和 SaveAndFlush
但没有成功。
这是一些代码:
Role superUser = Role.First(r => r.name == "Super User");
User me = User.First(r => r.UserName == myUserName);
me.Role = superUser;
me.Save(); // Or: SaveAndFlush
调试时,我可以在保存对象时看到对象的关联(即 me.Role
不为空并且具有正确的属性/属性/等)。但是,当我查看数据库,该用户的 RoleId
值仍然是 NULL
。 (SaveAndFlush
没有什么区别。)
我错过了什么?
我在 SO 上读到过,扩展用户表通常是通过添加另一个表并通过外键链接两个表来完成的;我假设这些类将通过组合使用继承来创建新的 ExtendedUser 类。假设我不想走那条路,为什么这行不通?是因为特定的 ASP.NET MVC 存储过程等吗?全部?
一些相关的映射:
[ActiveRecord("aspnet_Users", Mutable = false)]
public class User : ActiveRecordLinqBase<User>
{
[PrimaryKey(PrimaryKeyType.Assigned)]
public Guid UserId { get; set; }
// ...
[BelongsTo("RoleId", Cascade = CascadeEnum.SaveUpdate)]
public Role Role { get; set; }
}
[ActiveRecord]
public class Role : ActiveRecordLinqBase<Role>
{
[PrimaryKey]
public int Id { get; set; }
// ...
[HasMany(Inverse = true)]
public IList<User> Users { get; set; }
[Property]
public string Name { get; set; }
}
I'm using ActiveRecord with NHibernate on the backend. I've set up a mapping for Users; I can create/retrieve/register users without any issues.
I now want to add an association called Role
to my users (many users per role). I've created the appropriate Role
class, tables, data, etc. and everything seems to be working on that end as well.
The problem is that when I save a user and associate a Role
, that association does not persist to the database.
I've added a RoleId (int16)
column to the aspnet_Users
table to match the Role table's Id (int16)
column. I've tried using Save
and SaveAndFlush
without success.
Here's some code:
Role superUser = Role.First(r => r.name == "Super User");
User me = User.First(r => r.UserName == myUserName);
me.Role = superUser;
me.Save(); // Or: SaveAndFlush
When debugging, I can see the association on the objects when they're saved (i.e. me.Role
is not null and has the right attributes/properties/etc.) However, when I look at the database, the RoleId
value for that user is still NULL
. (SaveAndFlush
doesn't make a difference.)
What am I missing?
I've read somewhere on SO that extending the users table is usually done by adding another table and linking the two by a foreign key; I assume the classes would then use inheritance by composition for the new ExtendedUser
class. Assuming I don't want to go that route, why isn't this working? Is it because of the specific ASP.NET MVC stored procedures et. all?
Some relevant mapping:
[ActiveRecord("aspnet_Users", Mutable = false)]
public class User : ActiveRecordLinqBase<User>
{
[PrimaryKey(PrimaryKeyType.Assigned)]
public Guid UserId { get; set; }
// ...
[BelongsTo("RoleId", Cascade = CascadeEnum.SaveUpdate)]
public Role Role { get; set; }
}
[ActiveRecord]
public class Role : ActiveRecordLinqBase<Role>
{
[PrimaryKey]
public int Id { get; set; }
// ...
[HasMany(Inverse = true)]
public IList<User> Users { get; set; }
[Property]
public string Name { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑: mutable="false" - 这清楚地表明实体是只读的,这是问题的根源。
还:
我相信您需要定义级联。您不仅保存实体本身,还保存对其他实体的引用。使用属性、Fluent config 或 hbml 按照您需要的方式进行定义。以下是级联选项:
以下是每个级联选项的含义:
他们自己。
保存/更新多对多场景中的关联)。
delete-orphan - 当对象被删除时,删除关联中的所有对象。除此之外,当一个对象
从关联中删除并且不与另一个对象关联
(孤立),也将其删除。
您可能想阅读
Edit: mutable="false" - this clearly stands that entity is read only, which is the source of your problem.
Also:
I believe that you need to have cascading defined. You are not saving just the entity itself but also reference to other entity. Use attributes, fluent config or hbml to define this the way you need. Here are the cascading options:
Here is what each cascade option means:
themselves.
save/update the assoications in many-to-many scenario).
delete-orphan - when the object is deleted, delete all the objects in the assoication. In addition to that, when an object is
removed from the assoication and not assoicated with another object
(orphaned), also delete it.
You may want to read this article.