2011-12-23 47 views
0

我是nhibernate的新手。我使用的是nhibernate 3.這是我需要做的,選擇用戶它的Id是1.如果該用戶不存在,則返回null。Nhibernate,需要幫助來修復我的選擇查詢

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 [email protected] ]\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 [email protected]]"} 
[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 [email protected] ]\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 [email protected]]"} 
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 [email protected] ]\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 [email protected]]" 
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查詢,NhLinqExpression nhQuery個)\ r \ n在NHibernate.Linq.NhQueryProvider.Execute(表達式表達) \ r \ n在DevelopmentStack.Domain.Repositories上System.Linq.Queryable.FirstOrDefault [TSource](IQueryable`1源)\ r \ n處的NHibernate.Linq.NhQueryProvider.Execute [TResult](表達式表達式)\ r \ n C:\ code \ net \ DevelopmentStack \ DevelopmentStack.Domain \ Repositories \ Concrete \ NUserRepository.cs中的.Concrete.NUserRepository.AddUser(用戶用戶):第30行「 TargetSite:{System.Collections.IList DoList(NHibernate.Engine。 ISessionImplementor,NHibernate.Engine.QueryParameters)} >

回答

1

這可能是由於User是保留關鍵字。如果你在hbm中映射它,確保你已經在表名中提供了轉義:`用戶(用戶應該被「`由於降價而不能正確寫入)」或[用戶]。二者的區別在於第二個不關心方言。

0

嘗試:

var selectedUser = session.Get<User>(1); 

你應該總是使用GET而不是查詢其ID檢索實體時。如果實體不存在,則返回null。