我有一张旧表,结构如下:
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吗?
答案 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();
}
}