如何在NHibernate中创建双向映射

时间:2018-06-29 13:33:31

标签: c# nhibernate-mapping

我有三个表PersonAddressPersonAddressX已为其创建映射:

[Class(Nametype = typeof(Person), Table = "Person"]
public class Person 
{
    [Id(-2, Name = "Id")]
    [Generator(-1, Class = "native")]
    public virtual long Id { get; set; }

    [ManyToOne]
    public virtual Address Address { get; set; }
}

[Class(NameType = typeof(Address), Table = "Address")]
public class Address
{ 
    [Id(-2, Name = "Id")]
    [Generator(-1, Class = "native")]
    public virtual long Id { get; set; }

    [Property]
    public virtual string Street { get; set; }
}

[Class(NameType = typeof(PersonAddressX), Table = "PersonAddressX"]
public class PersonAddressX
{
    [Id(-2, Name = "Id")]
    [Generator(-1, Class = "native")]
    public virtual long Id { get; set; }

    [ManyToOne]
    public virtual Person Person { get; set; }

    [ManyToOne]
    public virtual Address Address { get; set; }
}

在我的应用程序中,我有一个方法Repository<Address>的{​​{1}},我想要从特定的GetPersonAddressX中获取IList<PersonAddressX>。使用当前映射,我无法做到这一点,因为它不是双向的。我尝试了一些双向映射的示例,但是结果查询返回Address.Id

0 个答案:

没有答案