我是nhibernate的新手。我正在使用的是nhibernate 3.这是我需要做的,选择User is Id is 1.如果该User不存在则返回null。
var selectedUser = session.Query()。其中(u => u.Id == 1).FirstOrDefault();
if (selectedUser == null)
{
userID = (int)session.Save(user);
}
我的选择查询没有。
这是我收到的错误消息。
could not execute query\r\n[ select TOP (@p0) user0_.Id as Id0_, user0_.Username as Username0_, user0_.Password as Password0_, user0_.CreateDate as CreateDate0_, user0_.Avatar as Avatar0_, user0_.AccountTypeId as AccountT6_0_, user0_.FirstName as FirstName0_, user0_.LastName as LastName0_, user0_.MiddleName as MiddleName0_, user0_.Email as Email0_, user0_.OtherId as OtherId0_ from [User] user0_ where user0_.Id=@p1 ]\r\n Name:p1 - Value:1\r\n[SQL: select TOP (@p0) user0_.Id as Id0_, user0_.Username as Username0_, user0_.Password as Password0_, user0_.CreateDate as CreateDate0_, user0_.Avatar as Avatar0_, user0_.AccountTypeId as AccountT6_0_, user0_.FirstName as FirstName0_, user0_.LastName as LastName0_, user0_.MiddleName as MiddleName0_, user0_.Email as Email0_, user0_.OtherId as OtherId0_ from [User] user0_ where user0_.Id=@p1]"}
[NHibernate.Exceptions.GenericADOException]: {"could not execute query\r\n[ select TOP (@p0) user0_.Id as Id0_, user0_.Username as Username0_, user0_.Password as Password0_, user0_.CreateDate as CreateDate0_, user0_.Avatar as Avatar0_, user0_.AccountTypeId as AccountT6_0_, user0_.FirstName as FirstName0_, user0_.LastName as LastName0_, user0_.MiddleName as MiddleName0_, user0_.Email as Email0_, user0_.OtherId as OtherId0_ from [User] user0_ where user0_.Id=@p1 ]\r\n Name:p1 - Value:1\r\n[SQL: select TOP (@p0) user0_.Id as Id0_, user0_.Username as Username0_, user0_.Password as Password0_, user0_.CreateDate as CreateDate0_, user0_.Avatar as Avatar0_, user0_.AccountTypeId as AccountT6_0_, user0_.FirstName as FirstName0_, user0_.LastName as LastName0_, user0_.MiddleName as MiddleName0_, user0_.Email as Email0_, user0_.OtherId as OtherId0_ from [User] user0_ where user0_.Id=@p1]"}
Data: {System.Collections.ListDictionaryInternal}
HelpLink: null
InnerException: {"Invalid object name 'User'."}
Message: "could not execute query\r\n[ select TOP (@p0) user0_.Id as Id0_, user0_.Username as Username0_, user0_.Password as Password0_, user0_.CreateDate as CreateDate0_, user0_.Avatar as Avatar0_, user0_.AccountTypeId as AccountT6_0_, user0_.FirstName as FirstName0_, user0_.LastName as LastName0_, user0_.MiddleName as MiddleName0_, user0_.Email as Email0_, user0_.OtherId as OtherId0_ from [User] user0_ where user0_.Id=@p1 ]\r\n Name:p1 - Value:1\r\n[SQL: select TOP (@p0) user0_.Id as Id0_, user0_.Username as Username0_, user0_.Password as Password0_, user0_.CreateDate as CreateDate0_, user0_.Avatar as Avatar0_, user0_.AccountTypeId as AccountT6_0_, user0_.FirstName as FirstName0_, user0_.LastName as LastName0_, user0_.MiddleName as MiddleName0_, user0_.Email as Email0_, user0_.OtherId as OtherId0_ from [User] user0_ where user0_.Id=@p1]"
Source: "NHibernate"
StackTrace: " at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)\r\n at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)\r\n at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)\r\n at NHibernate.Hql.Ast.ANTLR.Loader.QueryLoader.List(ISessionImplementor session, QueryParameters queryParameters)\r\n at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List(ISessionImplementor session, QueryParameters queryParameters)\r\n at NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters queryParameters, ISessionImplementor session, IList results)\r\n at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results)\r\n at NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters)\r\n at NHibernate.Impl.ExpressionQu
eryImpl.List()\ r \ n在NHibernate.Linq.NhQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression,IQuery query,NhLinqExpression nhQuery)\ r \ n在NHibernate.Linq.NhQueryProvider.Execute(表达式表达式)\ r \ n at NHibernate.Linq.NhQueryProvider.Execute [TResult](表达式)/ r \ n在System.Linq.Queryable.FirstOrDefault [TSource](IQueryable`1 source)\ r \ n在DevelopmentStack.Domain.Repositories.Concrete.NUserRepository .AddUser(用户用户)在C:\ code \ net \ DevelopmentStack \ DevelopmentStack.Domain \ Repositories \ Concrete \ NUserRepository.cs:第30行“ TargetSite:{System.Collections.IList DoList(NHibernate.Engine.ISessionImplementor,NHibernate.Engine.QueryParameters)} >
答案 0 :(得分:1)
这可能是因为User是一个保留关键字。如果你用hbm映射它,请确保你在表名中提供了转义:`用户(用户应该被“我因Markdown而无法正确写入”或 [User] 包围)。两者之间的区别在于第二个不关心方言。
答案 1 :(得分:0)
尝试:
var selectedUser = session.Get<User>(1);
在按ID检索实体时,应始终使用Get而不是查询。如果实体不存在则返回null。