仅一侧映射时查询多对多 nhibernate

发布于 2024-09-18 18:09:06 字数 1809 浏览 7 评论 0原文

我有以下实体

public class Client
{
    public virtual int Id{get;set;}
    public virtual IList<Telephone> Telephones { get; private set; }
}

public class User
{
    public virtual int Id{get;set;}
    public virtual IList<Telephone> Telephones { get; private set; }
}

public class Telephone
{
    public virtual int Id{get;set;}
    public virtual string Number { get; set; }
    public virtual string Extension { get; set; }
    public virtual TelephoneType TelephoneType { get; set; }
}

客户端作为这样的映射

HasManyToMany<Telephone>(x => x.Telephones)
.Table("tblClientTel")
.ParentKeyColumn("ClientId")
.ChildKeyColumn("TelId")
.LazyLoad()
.Cascade.SaveUpdate();

用户作为这样的映射

HasManyToMany<Telephone>(x => x.Telephones)
.Table("tblUserTel")
.ParentKeyColumn("UserId")
.ChildKeyColumn("TelId")
.LazyLoad()
.Cascade.SaveUpdate();

和电话这样

   public TelephoneMap()
    {
        Table("tblTel");
        Id(x => x.Id, "Id");
        LazyLoad();
        References<TelephoneType>(x => x.TelephoneType, "TypeId")
            .Not.Nullable()
            .Cascade.None()
            .Not.LazyLoad();
        Map(x => x.Number, "Number")
            .Not.Nullable()
            .Length(15);
        Map(x => x.Extension, "Extension")
            .Nullable()
            .Length(10);
    }

我如何查询客户列表的所有电话实体? 我已经尝试过

ICriteria criteria = base.CreateCriteria<Client>(null);
return base.CreateCriteria
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("Telephones"))
)
.Add(Expression.In("Id", clientIds))
.SetFetchMode("Telephones", FetchMode.Eager)
.List<Telephone>();

但它返回客户端 ID

I have the following entities

public class Client
{
    public virtual int Id{get;set;}
    public virtual IList<Telephone> Telephones { get; private set; }
}

public class User
{
    public virtual int Id{get;set;}
    public virtual IList<Telephone> Telephones { get; private set; }
}

public class Telephone
{
    public virtual int Id{get;set;}
    public virtual string Number { get; set; }
    public virtual string Extension { get; set; }
    public virtual TelephoneType TelephoneType { get; set; }
}

Client as mapping like this

HasManyToMany<Telephone>(x => x.Telephones)
.Table("tblClientTel")
.ParentKeyColumn("ClientId")
.ChildKeyColumn("TelId")
.LazyLoad()
.Cascade.SaveUpdate();

User as mapping like this

HasManyToMany<Telephone>(x => x.Telephones)
.Table("tblUserTel")
.ParentKeyColumn("UserId")
.ChildKeyColumn("TelId")
.LazyLoad()
.Cascade.SaveUpdate();

And Telephone like this

   public TelephoneMap()
    {
        Table("tblTel");
        Id(x => x.Id, "Id");
        LazyLoad();
        References<TelephoneType>(x => x.TelephoneType, "TypeId")
            .Not.Nullable()
            .Cascade.None()
            .Not.LazyLoad();
        Map(x => x.Number, "Number")
            .Not.Nullable()
            .Length(15);
        Map(x => x.Extension, "Extension")
            .Nullable()
            .Length(10);
    }

How can i query for all the telephone entities of a list of client ?
I've tried this

ICriteria criteria = base.CreateCriteria<Client>(null);
return base.CreateCriteria
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("Telephones"))
)
.Add(Expression.In("Id", clientIds))
.SetFetchMode("Telephones", FetchMode.Eager)
.List<Telephone>();

But it returns the client Id

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文