我可能有映射错误,或者其他什么,NHibernate似乎让我感到困惑,但是当它工作时它很顺利。到目前为止,Google已经完成了一切,我仍在学习NHibernate。我正在尝试查询具有accountId的地址,该地址将链接到我的AccountUser地图,该地图具有将链接到我的Usermap的userId。我想查找用户名为“joe”的所有地址。任何人都知道如何使用我的查询执行此操作?下面是我的地图,并查询:
public AddressMap()
{
Table("Address");
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.FirstName);
Map(x => x.MiddleName);
Map(x => x.LastName);
Map(x => x.CompanyName);
Map(x => x.Street);
Map(x => x.City);
Map(x => x.State);
Map(x => x.AccountID);
Map(x => x.Name);
References(x => x.AccountUser);
}
public class AccountUserMap: ClassMap<AccountUser>
{
public AccountUserMap()
{
Table("AccountUser");
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.UserId);
Map(x => x.AccountID).Not.Nullable();
References(x => x.User);
}
}
public UserMap()
{
Table("User");
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Name);
}
// non working query:
Address address = null;
User user = null;
AccountUser accountUser = null;
var query = session.QueryOver<Address>(() => address)
.JoinQueryOver(x => x.AccountUser, () => accountUser)
.Where(() => accountUser.AccountID == address.AccountID)
.JoinQueryOver(y => y.User, () => user)
.Where(() => user.Id == accountUser.UserId )
.And(() => user.Name == "joe")
.List()).ToList();
错误信息也不是很有用......
{“无效的列名'AccountUser_id'。\ r \ n无效的列名 'User_id'。\ r \ n无效列名'AccountUser_id'。\ r \ n无效列 名称'User_id'。“}
没有用下划线命名的东西,所以它必须是NHibernate
的东西此外,下面的SQL查询有效,这就是我想要做的事情:
SELECT TOP 1000
[AddressID]
,[FirstName]
,[MiddleName]
,[LastName]
FROM Address
inner join AccountUser accountUser
on AccountUser.AccountID = Address.AccountID
inner join User user
on user.UserID = accountUser.UserID
where user.Name= 'joe'
希望这有足够的细节
答案 0 :(得分:0)
如果属性名称与数据库名称不匹配,则必须在映射中指定它们,例如:
Id(x => x.Id, "AddressID").GeneratedBy.Native();
或Fluent NHibernate将使用其默认的键列命名约定。