Fluent NHibernate 对象化/关联关系映射

发布于 2024-09-30 12:53:12 字数 2247 浏览 0 评论 0原文

好吧,我对 Fluent 和 NHibernate 还很陌生,我不确定如何映射数据库中存在的这种特定关系。下面的 ER 图概述了我的表结构。

ER Diagram

下面是我当前的 C# 实体类和 Fluent 映射:

public class GroupHeader
{
    public virtual Guid Id { get; private set;}
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual IList<RightHeader> AllowedRights { get; set; }
    public virtual IList<RightHeader> DeniedRights { get; set; }
    public virtual IList<RightHeader> UnsetRights { get; set; }
}

public class RightHeader
{
    public virtual decimal Num { get; private set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
}

public class GroupHeaderMap : ClassMap<GroupHeader>
{
    public GroupHeaderMap()
    {
        Table("GROUP_HEADER");
        Id(x => x.Id, "GROUP_ID");
        Map(x => x.Name, "GROUP_NAME").Unique();
        Map(x => x.Description, "GROUP_DESCRIPTION");

        HasManyToMany(x => x.AllowedRights)
            .Table("GROUP_RIGHT_COMPOSITE")
            .ParentKeyColumn("GROUP_ID")
            .ChildKeyColumn("RIGHT_NUM")
            .Where("RIGHT_VALUE = 1")
            .Cascade.SaveUpdate();

        HasManyToMany(x => x.DeniedRights)
            .Table("GROUP_RIGHT_COMPOSITE")
            .ParentKeyColumn("GROUP_ID")
            .ChildKeyColumn("RIGHT_NUM")
            .Where("RIGHT_VALUE = -1")
            .Cascade.SaveUpdate();

        HasManyToMany(x => x.UnsetRights)
            .Table("GROUP_RIGHT_COMPOSITE")
            .ParentKeyColumn("GROUP_ID")
            .ChildKeyColumn("RIGHT_NUM")
            .Where("RIGHT_VALUE = 0")
            .Cascade.SaveUpdate();
    }
}

public class RightHeaderMap : ClassMap<RightHeader>
{
    public RightHeaderMap()
    {
        Table("RIGHT_HEADER");
        Id(x => x.Num, "RIGHT_NUM");
        Map(x => x.Name, "RIGHT_NAME");
        Map(x => x.Description, "RIGHT_DESCRIPTION");
    }
}

目前,每当我创建新的 GroupHeader 并填充时由于 GROUP_RIGHT_COMPOSITE 中的 RIGHT_VALUE 列不允许为空,AllowedRights 成员变量无法插入这些记录。我不太确定如何以流畅的方式映射它,或者我应该在我的实体中真正处理它。任何帮助将不胜感激。

Ok I'm pretty new to Fluent and NHibernate and I'm not sure how to map this specific relationship that exists in my database. I have the following ER diagram below that outlines my table structure.

ER Diagram

Below are my current C# entity classes and Fluent mappings:

public class GroupHeader
{
    public virtual Guid Id { get; private set;}
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual IList<RightHeader> AllowedRights { get; set; }
    public virtual IList<RightHeader> DeniedRights { get; set; }
    public virtual IList<RightHeader> UnsetRights { get; set; }
}

public class RightHeader
{
    public virtual decimal Num { get; private set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
}

public class GroupHeaderMap : ClassMap<GroupHeader>
{
    public GroupHeaderMap()
    {
        Table("GROUP_HEADER");
        Id(x => x.Id, "GROUP_ID");
        Map(x => x.Name, "GROUP_NAME").Unique();
        Map(x => x.Description, "GROUP_DESCRIPTION");

        HasManyToMany(x => x.AllowedRights)
            .Table("GROUP_RIGHT_COMPOSITE")
            .ParentKeyColumn("GROUP_ID")
            .ChildKeyColumn("RIGHT_NUM")
            .Where("RIGHT_VALUE = 1")
            .Cascade.SaveUpdate();

        HasManyToMany(x => x.DeniedRights)
            .Table("GROUP_RIGHT_COMPOSITE")
            .ParentKeyColumn("GROUP_ID")
            .ChildKeyColumn("RIGHT_NUM")
            .Where("RIGHT_VALUE = -1")
            .Cascade.SaveUpdate();

        HasManyToMany(x => x.UnsetRights)
            .Table("GROUP_RIGHT_COMPOSITE")
            .ParentKeyColumn("GROUP_ID")
            .ChildKeyColumn("RIGHT_NUM")
            .Where("RIGHT_VALUE = 0")
            .Cascade.SaveUpdate();
    }
}

public class RightHeaderMap : ClassMap<RightHeader>
{
    public RightHeaderMap()
    {
        Table("RIGHT_HEADER");
        Id(x => x.Num, "RIGHT_NUM");
        Map(x => x.Name, "RIGHT_NAME");
        Map(x => x.Description, "RIGHT_DESCRIPTION");
    }
}

Currently whenever I create a new GroupHeader and populate the AllowedRights member variable it fails to insert these records because the RIGHT_VALUE column in GROUP_RIGHT_COMPOSITE doesn't allow nulls. I'm not really sure how to map this in fluent or where I should really handle this in my entities. Any help would be appreciated.

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

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

发布评论

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

评论(1

说好的呢 2024-10-07 12:53:12

我在上述映射中发现的问题是,我试图将 USER_GROUP_COMPOSITE 表视为关系而不是实体本身。我修改了上面的代码,以便存在一个名为 GroupRight 的新实体,并且在该实体内引用 GroupHeader 和 RightHeader。

我认为这解决了我应该如何映射这些数据的总体问题,但我仍然在将这些数据保存到数据库时遇到问题。我修改后的问题(包括对我的实体和映射的更新)可以在此处找到 发布

我发现对解决此问题有用的文章是:
链接 1
链接 2

The problem I found with the above mapping is that I was trying to treat the USER_GROUP_COMPOSITE table as a relationship and not as an entity itself. I have modified the above code such that a new entity called GroupRight exists and has references to both GroupHeader and RightHeader inside the entity.

I think this solved my overall question of how I should map these but I am still having problems persisting this data to the database. My modified question (including updates to my entities and mappings) can be found here Post

The articles that I found useful in solving this issue were:
Link 1
Link 2

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