当我执行nHibernate LINQ查询时,我遇到了一个奇怪的异常。
如果我写
var address = new Address {Id = Guid.Empty};
session.Query().Where(x=> x == address).ToList();
比我得到
对象引用未保存的瞬态实例 - 保存瞬态 刷新之前的实例或为属性设置级联操作 会让它自动保存的东西。键入:DomainModel.Address, 实体:DomainModel.Address
在 NHibernate.Engine.ForeignKeys.GetEntityIdentifierIfNotUnsaved(字符串 entityName,Object entity,ISessionImplementor session)at NHibernate.Type.EntityType.GetIdentifier(对象值, ISessionImplementor会议) NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand cmd,Object value,Int32 index,ISessionImplementor session)at NHibernate.Param.NamedParameterSpecification.Bind(IDbCommand命令, IList
1 multiSqlQueryParametersList, Int32 singleSqlParametersOffset, IList
1 sqlQueryParametersList,QueryParameters queryParameters, ISessionImplementor会议) NHibernate.Param.NamedParameterSpecification.Bind(IDbCommand命令, IList1 sqlQueryParametersList, QueryParameters queryParameters, ISessionImplementor session) at NHibernate.SqlCommand.SqlCommandImpl.Bind(IDbCommand command, ISessionImplementor session) at NHibernate.Loader.Loader.PrepareQueryCommand(QueryParameters queryParameters, Boolean scroll, ISessionImplementor session) at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet
1个querySpaces,IType [] resultTypes)at NHibernate.Hql.Ast.ANTLR.Loader.QueryLoader.List(ISessionImplementor session,QueryParameters queryParameters)at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List(ISessionImplementor session,QueryParameters queryParameters)at NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters queryParameters,ISessionImplementor session,IList results)at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters,IList results)at NHibernate.Impl.AbstractSessionImpl.List(IQueryExpression queryExpression,QueryParameters参数)at NHibernate.Impl.ExpressionQueryImpl.List()at NHibernate.Linq.DefaultQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression,IQuery查询,NhLinqExpression nhQuery)at NHibernate.Linq.DefaultQueryProvider.Execute(表达式) 在NHibernate.Linq.DefaultQueryProvider.Execute [TResult](表达式 表达式)在Remotion.Linq.QueryableBase1.GetEnumerator() at System.Linq.Buffer
1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable
1来源)
但是,如果我写
var person = new Person {Id = Guid.Empty};
session.Query().Where(x=> x == person).ToList();
我知道我可以使用Key字段而不是对象重写查询,但这只是问题的真正简化版本,此解决方案不适用。
对这种行为有什么看法吗?
谢谢, 马可
答案 0 :(得分:0)