Microsoft.OData.Client.DataServiceCollection<T> 异常:尚未为 类型的实体提供实体集名称

AKo*_*ich 2 c# entity-framework odata

我在使用 Microsoft.OData.Client.DataServiceCollection 实例并对其调用 Load(...) 或 Add(...) 时遇到以下异常:尚未为类型 的实体提供实体集名称。

例如:

DataServiceCollection<TEntity> collection = 
new DataServiceCollection<TEntity>(_repoDataServiceQuery);
collection.Load(entity);
Run Code Online (Sandbox Code Playgroud)

关于我为了避免异常而缺少什么的任何建议?

Jun*_*ynn 5

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

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

构造函数是

public DataServiceCollection(DataServiceContext context, string entitySetName, Func<EntityChangedParams, bool> entityChangedCallback, Func<EntityCollectionChangedParams, bool> collectionChangedCallback);
Run Code Online (Sandbox Code Playgroud)

最后两个 Func 可以简单地是null.