将审核列添加到 Fluent NHibernate 多对多联结表

发布于 2024-09-29 01:48:10 字数 468 浏览 7 评论 0原文

我正在使用 Fluent NHibernate 从 .Net 实体类生成数据库架构。我有两个具有多对多关系的类 UserPermission,Fluent NHibernate 在数据库中正确生成了一个联结表 UsersToPermissions

正如预期的那样,联结表存储主键 UserIdPermissionId。我想要的是将审核信息附加到该表,例如 CreatedDate 和 UpdatedDate。我已经使用拦截器在非联结表上实现了此功能,如 NHibernate 文档< /a>.

如何在 UsersToPermissions 表上实现审核列?

I'm using Fluent NHibernate to generate a database schema from .Net entity classes. I have two classes User and Permission with a many to many relationship, and Fluent NHibernate is correctly generating a junction table UsersToPermissions in the database.

As expected the junction table is storing the primary keys UserId and PermissionId. What I am wanting is to also have auditing information attached to this table such as CreatedDate and UpdatedDate. I have already implemented this on the non junction tables using interceptors as described in the NHibernate documentation.

How can I implement audit columns on the UsersToPermissions table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

酷到爆炸 2024-10-06 01:48:10

直接不可能,因为关系没有属性。

最简单的方法是将联结表映射为实体,并使用 LINQ-to-objects 投影边以方便使用。

例如,假设我们将中间实体称为UserPermission

class User
{
    // Real relationship, mapped as bag or set with one-to-many UserPermission
    protected virtual ICollection<UserPermission> UserPermissions { get; set; }

    public virtual IEnumerable<Permission> Permissions
    {
        get { return from up in UserPermissions select up.Permission; }
    }

    public void Add(Permission permission)
    {
        UserPermissions.Add(new UserPermission
                            {
                                User = this,
                                Permission = permission
                            });
    }
}

如果需要,Permission 可以具有完全相同的权限。

It's not possible directly, because relationships don't have properties.

The easiest way is to map the junction table as an entity, and project the sides using LINQ-to-objects for ease of use.

For example, assuming we call the intermediate entity UserPermission:

class User
{
    // Real relationship, mapped as bag or set with one-to-many UserPermission
    protected virtual ICollection<UserPermission> UserPermissions { get; set; }

    public virtual IEnumerable<Permission> Permissions
    {
        get { return from up in UserPermissions select up.Permission; }
    }

    public void Add(Permission permission)
    {
        UserPermissions.Add(new UserPermission
                            {
                                User = this,
                                Permission = permission
                            });
    }
}

Permission can have exactly the same if needed.

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