3

我的网站上有一个搜索功能,有时会失败。它在 C# / ASP.NET / WebForms 中构建并使用 ActiveRecord。

基本上,当我从我的数据库中的 VIEW 返回太多记录时,我会收到 en UPDATE 错误。

string query = "s";
Order[] orders = new order[] { Order.Asc("LastName") };
List<ICriterion> crit = new List<ICriterion>();
Crit.Add(Expression.Sql(String.Format("LastName" LIKE '%{0}%' OR FirstName LIKE '%{0}%'", query)));
var rv = ActiveRecordBase.FindAll(typeof(vMyView), orders, crit.ToArray())

当使用“s”作为查询进行搜索时,我的代码的上述部分在 FindAll 命令上失败(我会得到很多结果)。但是使用“x”作为查询它就像一个魅力。

我得到的错误是:

View or function 'vAllowedVessels' is not updatable because the modification affects multiple base tables. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: View or function 'vAllowedVessels' is not updatable because the modification affects multiple base tables.

Source Error: 

Line 74:             List<ICriterion> crit = new List<ICriterion>();
Line 75:             Crit.Add(Expression.Sql(String.Format("LastName" LIKE '%{0}%' OR FirstName LIKE '%{0}%'", query)));
Line 75:             var rv = ActiveRecordBase.FindAll(typeof(vMyView), orders, crit.ToArray()) <--- This is highlighted in red!

Stack Trace:
[GenericADOException: could not update: [Ovf.Model.vAllowedVessels#3843][SQL: UPDATE vAllowedVessels SET FixtureId = ?, ContractDate = ?, No = ?, ContractForm = ?, VesselName = ?, Status = ?, CreatedDate = ?, Buyer = ?, BuyerName = ?, BuyerShortName = ?, BuyPrincipal = ?, BuyPrincipalName = ?, Seller = ?, SellerName = ?, SelPrincipal = ?, SelPrincipalName = ?, Operator = ?, Broker = ?, UsrCode = ? WHERE VesselFolderId = ?]]
NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session) +2474
NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session) +335
NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFields, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, Object rowId, ISessionImplementor session) +1898
NHibernate.Action.EntityUpdateAction.Execute() +764
NHibernate.Engine.ActionQueue.Execute(IExecutable executable) +48
NHibernate.Engine.ActionQueue.ExecuteActions(IList list) +128
NHibernate.Engine.ActionQueue.ExecuteActions() +50
NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) +215
NHibernate.Event.Default.DefaultAutoFlushEventListener.OnAutoFlush(AutoFlushEvent event) +225
NHibernate.Impl.SessionImpl.AutoFlushIfRequired(ISet`1 querySpaces) +288
NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) +524
NHibernate.Impl.CriteriaImpl.List(IList results) +76
NHibernate.Impl.CriteriaImpl.List() +71
Castle.ActiveRecord.ActiveRecordBase.FindAll(Type targetType, Order[] orders, ICriterion[] criteria) in c:\TeamCity\buildAgent\work\e41ee5ead2eba140\src\Castle.ActiveRecord\Framework\ActiveRecordBase.cs:1034

[ActiveRecordException: Could not perform FindAll for vAllowedVessels]
Castle.ActiveRecord.ActiveRecordBase.FindAll(Type targetType, Order[] orders, ICriterion[] criteria) in c:\TeamCity\buildAgent\work\e41ee5ead2eba140\src\Castle.ActiveRecord\Framework\ActiveRecordBase.cs:1046
Ovf.Model.vAllowedVessels.SearchByType(String query, String typename, String usrCode) in g:\Dev\2012\OnTrack\model\Profile\vAllowedVessels.cs:76
Search_Default.BindVesselFolders() in g:\Dev\2012\OnTrack\webapp\Search\Default.aspx.cs:155
Search_Default.ChangeStatus(Object sender, EventArgs e) in g:\Dev\2012\OnTrack\webapp\Search\Default.aspx.cs:145
System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e) +116
System.Web.UI.WebControls.DropDownList.RaisePostDataChangedEvent() +133
System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +13
System.Web.UI.Page.RaiseChangedEvents() +132
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1644

应该注意的是,我不想更新任何东西 - 只需查询我的 VIEW。

4

1 回答 1

3

显然,您的某些数据看起来对 NH 很脏,即使您没有进行任何更改,也需要保存。这可能来自正在转换值的属性获取器或自定义用户类型,因此它们不再匹配从数据库中出来的值。例如 - 这样的代码可能有问题:

private string _lastName;
public virtual string LastName
{
    get { return _lastName.Trim(); }
    set { _lastName = value; }
}

即使您从未接触过 setter,该属性的值仍然看起来像已更改。看看您是否可以删除这些类型的转换 - 以另一种方式处理它们。

这是处理这个问题的另一种可能更简单的方法:由于您无法通过此 SQL 视图保存更改,因此您应该添加mutable="false"到类映射中。请参阅文档

对不可变类 的更改mutable="false"将不会被持久化。

在 Fluent NHibernate 中,您使用ReadOnly()方法来控制此mutable标志。

于 2013-08-30T16:27:21.780 回答