Microsoft.OData.Client.DataServiceCollection< T>类型的实体没有提供实体集

本文关键字:实体 类型 Client OData DataServiceCollection Microsoft | 更新日期: 2023-09-27 18:12:18

在使用Microsoft.OData.Client.DataServiceCollection实例并调用Load(…)或Add(…)时,我得到以下异常:类型的实体没有提供实体集名称。

例如:

DataServiceCollection<TEntity> collection = 
new DataServiceCollection<TEntity>(_repoDataServiceQuery);
collection.Load(entity);

有什么建议,我错过了什么,以避免异常?

Microsoft.OData.Client.DataServiceCollection< T>类型的实体没有提供实体集

是_repoDataServicerQuery的实体集或实体的IEnumerable你从服务器的查询?在这种情况下,您不需要在构造DataServiceCollection时提供entityset名称,它将为您查找并设置它,因为查询或项中包含了足够的信息。否则,如果您只想要一个空的DataServiceCollection来添加或加载实体,您需要设置entityset名称来告诉它您想要添加到哪个实体集或从哪个实体集加载。例如:

DataServiceCollection<Customer> customers =
    new DataServiceCollection<Customer>(context, "Customers"/*entityset name*/, null, null);
var customer = new Customer();
customers.Add(customer);

构造函数

public DataServiceCollection(DataServiceContext context, string entitySetName, Func<EntityChangedParams, bool> entityChangedCallback, Func<EntityCollectionChangedParams, bool> collectionChangedCallback);

最后两个函数可以简单地为null