无法通过 NHibernate/ActiveRecord 将字段保留到 Aspnet_Users

发布于 2024-12-19 12:06:32 字数 1600 浏览 0 评论 0原文

我在后端使用 ActiveRecord 和 NHibernate。我已经为用户设置了映射;我可以毫无问题地创建/检索/注册用户。

我现在想要向我的用户添加一个名为 Role 的关联(每个角色有许多用户)。我已经创建了适当的 Role 类、表、数据等,并且一切似乎都在为此目的工作。

问题是,当我保存用户并关联角色时,该关联不会保留到数据库中。

我已将 RoleId (int16) 列添加到 aspnet_Users 表中,以匹配 Role 表的 Id (int16) 列。我尝试使用 SaveSaveAndFlush 但没有成功。

这是一些代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

树深时见影 2024-12-26 12:06:32

编辑: mutable="false" - 这清楚地表明实体是只读的,这是问题的根源。

不可变类(mutable="false")可能无法由应用程序更新或删除。这允许 NHibernate 做一些小的
性能优化。

还:
我相信您需要定义级联。您不仅保存实体本身,还保存对其他实体的引用。使用属性、Fluent config 或 hbml 按照您需要的方式进行定义。以下是级联选项:

以下是每个级联选项的含义:

  • none - 不执行任何级联,让用户通过以下方式处理它们
    他们自己。
  • 保存更新 - 保存/更新对象时,检查关联并保存/更新任何需要它的对象(包括
    保存/更新多对多场景中的关联)。
  • 删除 - 删除对象时,删除关联中的所有对象。
    delete-orphan - 当对象被删除时,删除关联中的所有对象。除此之外,当一个对象
    从关联中删除并且不与另一个对象关联
    (孤立),也将其删除。
  • all - 当保存/更新/删除对象时,检查关联并保存/更新/删除找到的所有对象。
  • all-delete-orphan - 当保存/更新/删除对象时,检查关联并保存/更新/删除找到的所有对象。除此之外,当一个对象从关联中删除并且不与另一个对象(孤立的)关联时,也将其删除。

您可能想阅读

Edit: mutable="false" - this clearly stands that entity is read only, which is the source of your problem.

Immutable classes, mutable="false", may not be updated or deleted by the application. This allows NHibernate to make some minor
performance optimizations.

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:

  • none - do not do any cascades, let the users handles them by
    themselves.
  • save-update - when the object is saved/updated, check the assoications and save/update any object that require it (including
    save/update the assoications in many-to-many scenario).
  • delete - when the object is deleted, delete all the objects in the assoication.
    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.
  • all - when an object is save/update/delete, check the assoications and save/update/delete all the objects found.
  • all-delete-orphan - when an object is save/update/delete, check the assoications and save/update/delete all the objects found. In additional 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文