简体   繁体   English

如何加入NHibernate / Fluent?

[英]How to join in NHibernate/Fluent?

This is so simple that I bet this has to be a duplicate question. 这很简单,我敢打赌这一定是重复的问题。

I have Dog and Cat , which are mapped to tables: 我有DogCat ,它们映射到表:

class Dog
{
    public virtual long Id;
    public virtual long catId;
}

class Cat
{
    public virtual long Id;
    public virtual string Name;
}

What I want is to obtain every Dog with Cat's name, instead of the id: 我想要的是获取每只具有猫名而不是ID的狗:

class DogWithName
{
    public virtual long Id;
    public virtual string CatName;
}

So DogWithName should be the result of a Dog and Cat joined query. 所以DogWithName应该是Dog and Cat加入查询的结果。 I thought about creating a mapping for DogWithName , but that means that a DogWithName table will be created in the schema. 我考虑过为DogWithName创建映射,但这意味着将在架构中创建DogWithName表。

I don't want that. 我不要 I only want a dynamic query that returns every Dog with the referenced Cat's name. 我只想要一个动态查询,该查询返回每个带有引用的猫的名字的狗。 How should I resolve this? 我该如何解决?

var result = from d in Session.Query<Dogs>() select new DogWithName { Id = d.Id, CatName = d.Cat.Name }

You have to map your classes though. 但是,您必须映射您的类。 The mappings are what define the way the tables are joined. 映射是定义表连接方式的对象。

I'd also change your Dog class to 我也将您的Dog课程更改为

public class Dog
{
    public virtual long Id { get; set; }
    public virtual Cat Cat { get; set; }
}

you then need to map the Cat as a ManyToOne relationship. 然后,您需要将Cat映射为ManyToOne关系。 You haven't provided any mappings? 您没有提供任何映射?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM