![](/img/trans.png)
[英]The option to add an ADO.net data entity to my SL business application is not there. How can I find it?
[英]SL3/SL4 - Ado.Net Data Services Error during new DataServiceCollection<T>(queryResponse)
大家好,我在SL项目(VS2010)中有两个功能几乎可以完成相同的操作,但是其中一个会引发错误,而另一个则不会。 它似乎与预测有关,但我不确定最好的解决方法。
起作用的功能是...
public void LoadAllChunksExpandAll(DataHelperReturnHandler handler, string orderby)
{
DataServiceCollection<CmsChunk> data = null;
DataServiceQuery<CmsChunk> theQuery = _dataservice
.CmsChunks
.Expand("CmsItemState")
.AddQueryOption("$orderby", orderby);
theQuery.BeginExecute(
delegate(IAsyncResult asyncResult)
{
_callback_dispatcher.BeginInvoke(
() =>
{
try
{
DataServiceQuery<CmsChunk> query = asyncResult.AsyncState as DataServiceQuery<CmsChunk>;
if (query != null)
{
//create a tracked DataServiceCollection from the result of the asynchronous query.
QueryOperationResponse<CmsChunk> queryResponse =
query.EndExecute(asyncResult) as QueryOperationResponse<CmsChunk>;
data = new DataServiceCollection<CmsChunk>(queryResponse);
handler(data);
}
}
catch
{
handler(data);
}
}
);
},
theQuery
);
}
这将编译并按预期运行。 一个非常非常相似的功能(如下所示)失败了...
public void LoadAllPagesExpandAll(DataHelperReturnHandler handler, string orderby)
{
DataServiceCollection<CmsPage> data = null;
DataServiceQuery<CmsPage> theQuery = _dataservice
.CmsPages
.Expand("CmsChildPages")
.Expand("CmsParentPage")
.Expand("CmsItemState")
.AddQueryOption("$orderby", orderby);
theQuery.BeginExecute(
delegate(IAsyncResult asyncResult)
{
_callback_dispatcher.BeginInvoke(
() =>
{
try
{
DataServiceQuery<CmsPage> query = asyncResult.AsyncState as DataServiceQuery<CmsPage>;
if (query != null)
{
//create a tracked DataServiceCollection from the result of the asynchronous query.
QueryOperationResponse<CmsPage> queryResponse = query.EndExecute(asyncResult) as QueryOperationResponse<CmsPage>;
data = new DataServiceCollection<CmsPage>(queryResponse);
handler(data);
}
}
catch
{
handler(data);
}
}
);
},
theQuery
);
}
显然,问题在于涉及自我引用关系的扩展投影(页面可以包含其他页面)。 这是在SL4或SL3下,使用ADONETDataServices SL3更新CTP3。
我乐于接受任何解决方法或指向粘性信息的指示,对错误的Google搜索会导致两次点击,但我都无法破译它们。
简短错误是“无法将项目添加到集合中。当DataServiceContext跟踪DataServiceCollection中的项目时,在将项目加载到集合中之前无法添加新项目。”
完整的错误是...
捕获了System.Reflection.TargetInvocationException消息=调用的目标已抛出异常。 StackTrace:在System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo方法,对象目标,Object []参数,SignatureStruct&sig,MethodAttributes方法属性,RuntimeType类型所有者)在System.Reflection.RuntimeMethodInfo.Invoke(对象obj,BindingFlags invokeAttr,活页夹活页夹,对象[] System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,活页夹活页夹,Object []参数,CultureInfo文化)的System.Reflection.MethodBase.Invoke(Object obj,Object []参数)中的参数,CultureInfo文化,布尔skipVisibilityChecks) ),位于System.Data.Services.Client.AtomMaterializer.ApplyItemsToCollection(AtomEntry条目,ClientProperty属性,IEnumerable项,位于System.Data.Services.Client.ClientType.ClientProperty.SetValue(对象实例,对象值,字符串propertyName,布尔值allowAdd) Uri nextLink,位于System.Data.Services.Client.AtomMaterializer.ApplyFeedToCollection(AtomEntry条目,ClientProperty的ProjectionPlancontinuationPlan) 属性,位于System.Data.Services.Client.AtomMaterializer.MaterializeResolvedEntry(AtomEntry条目,布尔值包含链接)的System.Data.Services.Client.AtomMaterializer.Materialize(AtomEntry条目,类型ExpectedEntryType,布尔值includeLinks) System.Data.Services.Client.AtomMaterializerInvoker.DirectMaterializePlan(对象Materializer,对象条目,类型ExpectedEntryType)在System.Data.Services处的System.Data.Services.Client.AtomMaterializer.DirectMaterializePlan(AtomMaterializer实体化程序,AtomEntry条目,类型ExpectedEntryType)。在System.Data.Services.Client.AtomMaterializer.Read()在System.Data.Services.Client.MaterializeAtom.MoveNextInternal()在System.Data.Services处的Client.ProjectionPlan.Run(AtomMaterializer实现器,AtomEntry条目,类型ExpectedType)。 System.Linq.Enumerable.d__b1的
1.MoveNext() at System.Data.Services.Client.DataServiceCollection
处的1.MoveNext() at System.Data.Services.Client.DataServiceCollection
1.InternalLoadCollection(IEnumerable1 items) at System.Data.Services.Client.DataServiceCollection
1 items) at System.Data.Services.Client.DataServiceCollection
1..ctor(1 items) at System.Data.Services.Client.DataServiceCollection
1.StartTracking(DataServiceContext上下文,IEnumerable1 items, String entitySet, Func
函数2实体更改,函数2 collectionChanged) at System.Data.Services.Client.DataServiceCollection
IEnumerable1 items, TrackingMode trackingMode, String entitySetName, Func
2 EntityChangedCallback,Func2 collectionChangedCallback) at System.Data.Services.Client.DataServiceCollection
(2 collectionChangedCallback) at System.Data.Services.Client.DataServiceCollection
1..ctor(IEnumerable1 items) at Phinli.Dashboard.Silverlight.Helpers.DataHelper.<>c__DisplayClass44.<>c__DisplayClass46.<LoadAllPagesExpandAll>b__43() InnerException: System.InvalidOperationException Message=An item could not be added to the collection. When items in a DataServiceCollection are tracked by the DataServiceContext, new items cannot be added before items have been loaded into the collection. StackTrace: at System.Data.Services.Client.DataServiceCollection
1 items) at Phinli.Dashboard.Silverlight.Helpers.DataHelper.<>c__DisplayClass44.<>c__DisplayClass46.<LoadAllPagesExpandAll>b__43() InnerException: System.InvalidOperationException Message=An item could not be added to the collection. When items in a DataServiceCollection are tracked by the DataServiceContext, new items cannot be added before items have been loaded into the collection. StackTrace: at System.Data.Services.Client.DataServiceCollection
1 items) at Phinli.Dashboard.Silverlight.Helpers.DataHelper.<>c__DisplayClass44.<>c__DisplayClass46.<LoadAllPagesExpandAll>b__43() InnerException: System.InvalidOperationException Message=An item could not be added to the collection. When items in a DataServiceCollection are tracked by the DataServiceContext, new items cannot be added before items have been loaded into the collection. StackTrace: at System.Data.Services.Client.DataServiceCollection
1.InsertItem(Int32索引,T项目)位于System.Collections.ObjectModel.Collection`1.Add(T项目)InnerException:
谢谢你的帮助!
我认为这里的问题是DataServiceCollection无法将下载的实体添加到顶级CMSPage类型的相应DataServiceCollection属性中。我们做了一些特殊的说明以确保DataServiceCollection仅在开始跟踪某些对象之后才能使用。 我稍后将对此进行调查,同时尝试以下代码:
public void LoadAllPagesExpandAll(DataHelperReturnHandler handler, string orderby)
{
DataServiceCollection<CmsPage> data = new DataServiceCollection<CmsPage>( _dataservice );
DataServiceQuery<CmsPage> theQuery = _dataservice
.CmsPages
.Expand("CmsChildPages")
.Expand("CmsParentPage")
.Expand("CmsItemState")
.AddQueryOption("$orderby", orderby);
theQuery.BeginExecute(
delegate(IAsyncResult asyncResult)
{
_callback_dispatcher.BeginInvoke(
() =>
{
try
{
DataServiceQuery<CmsPage> query = asyncResult.AsyncState as DataServiceQuery<CmsPage>;
if (query != null)
{
//create a tracked DataServiceCollection from the result of the asynchronous query.
QueryOperationResponse<CmsPage> queryResponse = query.EndExecute(asyncResult) as QueryOperationResponse<CmsPage>;
data.Load(queryResponse);
handler(data);
}
}
catch
{
handler(data);
}
}
);
},
theQuery
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.