Fluent NHibernate:如何在 ManyToMany 上映射 where 子句过滤器

发布于 2024-12-02 02:23:47 字数 2063 浏览 3 评论 0原文

我在保留一个字段时遇到问题,该字段位于多对多关系的 were 子句中,但在类映射中没有明确存在。很难解释,但它是下面的类和映射中的“is_expred”字段。

我有以下两个类:

public class Publication
{
    public virtual int Id {get; set;}

    public virtual string Name {get; set;}
}

public class Role
{
    public virtual int Id {get; set;}
    public virtual string Name {get; set;}

    public virtual IEnumerable<Publication> CurrentPublications {get; set;}

    public virtual IEnumerable<Publication> ExpiredPublications {get; set;}
}

其中出版物可以存在于 CurrentPublications 和 ExpiredPublications 中的一个或两个中。 (不,我不能将过期字段放在出版物上,因为它仅针对该特定角色过期 - 它可能是当前的不同角色)

此数据库是:

role
{
    role_id     int (PK)
    role_name   varchar(50)
}

role_pub
{
    role_id     int (PK)
    pub_id      int (PK)
    is_expired  bit
}

pub
{
    pub_id      int (PK)
    pub_name    varchar(50)
}

流畅的映射:

public class RoleMapping : ClassMap<Role>
{
    public RoleMapping()
    {
        Table("role");
        Id(x => x.Id, "role_id").GeneratedBy.Identity();
        Map(x => x.Name, "role_name").Not.Nullable();

        HasManyToMany<Publication>(x => x.CurrentPublications)
            .Table("role_pub")
            .ParentKeyColumn("role_id")
            .ChildKeyColumn("pub_id")
            .Where("is_expired = 0")
            .LazyLoad();

        HasManyToMany<Publication>(x => x.ExpiredPublications)
            .Table("role_pub")
            .ParentKeyColumn("role_id")
            .ChildKeyColumn("pub_id")
            .Where("is_expired = 1")
            .LazyLoad();
    }
}


public class PublicationMapping : ClassMap<Publication>
{
    public PublicationMapping()
    {
        Table("pub");
        Id(x => x.Id, "pub_id").GeneratedBy.Identity();
        Map(x => x.Name, "pub_name").Not.Nullable();
    }
}

当我执行选择时在角色上,当前和过期出版物填充了正确的出版物,但是当我将新出版物添加到当前或过期列表时,它总是将 is_expired 字段保存为 0,这是默认值数据库中的“is_expired”。

有谁知道映射此关系并正确填充“is_expired”字段的正确方法是什么?

感谢您的帮助

萨恩

I'm having an issue with persisting a field which is in my were clause on a ManyToMany releationship, but doesn't explicityly exist on the class mapping. Hard to explain but its the "is_expred" field in the below classes and mappings.

I have the following two classes:

public class Publication
{
    public virtual int Id {get; set;}

    public virtual string Name {get; set;}
}

public class Role
{
    public virtual int Id {get; set;}
    public virtual string Name {get; set;}

    public virtual IEnumerable<Publication> CurrentPublications {get; set;}

    public virtual IEnumerable<Publication> ExpiredPublications {get; set;}
}

Where a publication can exist in either or both the CurrentPublications and ExpiredPublications. (and no, I can't put the expired field on the Publication, as its only expired for this particular role - it might be current on a different role)

The DB for this is:

role
{
    role_id     int (PK)
    role_name   varchar(50)
}

role_pub
{
    role_id     int (PK)
    pub_id      int (PK)
    is_expired  bit
}

pub
{
    pub_id      int (PK)
    pub_name    varchar(50)
}

And the fluent mapping:

public class RoleMapping : ClassMap<Role>
{
    public RoleMapping()
    {
        Table("role");
        Id(x => x.Id, "role_id").GeneratedBy.Identity();
        Map(x => x.Name, "role_name").Not.Nullable();

        HasManyToMany<Publication>(x => x.CurrentPublications)
            .Table("role_pub")
            .ParentKeyColumn("role_id")
            .ChildKeyColumn("pub_id")
            .Where("is_expired = 0")
            .LazyLoad();

        HasManyToMany<Publication>(x => x.ExpiredPublications)
            .Table("role_pub")
            .ParentKeyColumn("role_id")
            .ChildKeyColumn("pub_id")
            .Where("is_expired = 1")
            .LazyLoad();
    }
}


public class PublicationMapping : ClassMap<Publication>
{
    public PublicationMapping()
    {
        Table("pub");
        Id(x => x.Id, "pub_id").GeneratedBy.Identity();
        Map(x => x.Name, "pub_name").Not.Nullable();
    }
}

When I do the select on a role, the Current and Expired Publications are populated with the correct Publications, but when I add new publciations to either Current or Expired lists, it always saves the is_expired field as 0, which is the default value of "is_expired" in the DB.

Does anyone have any idea on wha tthe correct way of mapping this releationship and populating the "is_expired" field correctly?

Thanks for your help

Saan

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

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

发布评论

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

评论(1

Spring初心 2024-12-09 02:23:47

where 仅在 SELECT 上进行过滤,但绝不会填充 INSERT 上的列。您可以引入一个类 RolePublication ,其中映射了 is_expired

public class RolePublication
{
    public virtual Role Role {get; set;}
    public virtual Publication Publication {get; set;}
    public virtual bool IsExpired {get; set;}
}

public class Role
{
    ...
    internal protected virtual ICollection<RolePublication> Publications {get; set;}

    public virtual IEnumerable<Publication> CurrentPublications
    { get { return Publications.Where(rp => rp.IsExpired == false).Select(rp => rp.Publication); ) } }

    public virtual IEnumerable<Publication> ExpiredPublications
    { get { return Publications.Where(rp => rp.IsExpired == true).Select(rp => rp.Publication); ) } }
}

the where only filters on SELECT but never fills up the columns on INSERT. you could introduce a class RolePublication which has the is_expired mapped

public class RolePublication
{
    public virtual Role Role {get; set;}
    public virtual Publication Publication {get; set;}
    public virtual bool IsExpired {get; set;}
}

public class Role
{
    ...
    internal protected virtual ICollection<RolePublication> Publications {get; set;}

    public virtual IEnumerable<Publication> CurrentPublications
    { get { return Publications.Where(rp => rp.IsExpired == false).Select(rp => rp.Publication); ) } }

    public virtual IEnumerable<Publication> ExpiredPublications
    { get { return Publications.Where(rp => rp.IsExpired == true).Select(rp => rp.Publication); ) } }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文