Nhibernate映射大师 - >来自同一表的详细信息类

时间:2015-07-20 16:09:13

标签: c# database nhibernate orm

我有一张旧表,结构如下:

UserId GUID
SelectionId GUID 
DateSelected DATETIME

将它映射到单个类很容易,但我想将它映射到两个类,因为我总是将这个表排队到UserId

public class User
{
    Guid UserId {get;set;}
    IEnumerable<UserSelection> UserSelection {get;set;}
}

public class UserSelection
{
    public Guid SelectionId { get; set;}
    public DateTime DateSelected { get; set;}
}

我有可能在NHibernate吗?

1 个答案:

答案 0 :(得分:0)

我还没有尝试,但你可以使用 subselect feature使用映射到查询,将是这样的:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Subselect("SELECT DISTINCT UserId FROM OldTable");

        ReadOnly();

        Id(x => x.UserId);

        HasMany(x => x.UserSelection).Table("OldTable").KeyColumn("UserId");
    }
}

public class UserSelectionMap : ClassMap<UserSelection>
{
    public UserSelectionMap()
    {
        Table("OldTable");

        Id(x => x.SelectionId);

        Map(x => x.DateSelected);

        References(x => x.User).Column("UserId").Not.Nullable();
    }
}