编程语言
首页 > 编程语言> > 首页> C#> ActiveRecord的/ NHibernate的:更新查询错误

首页> C#> ActiveRecord的/ NHibernate的:更新查询错误

作者:互联网

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

基本上,当我从数据库中的VIEW返回的记录太多时,就会出现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.

解决方法:

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

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

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

这是另一种可能更简单的方法:由于无法通过此SQL视图保存更改,因此应将mutable =“ false”添加到类映射.参见documentation

Changes to immutable classes, mutable="false", will not be persisted.

在Fluent NHibernate中,您可以使用ReadOnly()方法来控制此可变标志.

标签:activerecord,nhibernate,asp-net,c
来源: https://codeday.me/bug/20191122/2063032.html