问题描述
我通过使用方法 fetchEntityByKey
获得单个实体,此后,我通过 entityAspect.loadNavigationProperty加载该实体的导航属性
。但是 loadNavigationProperty
总是对服务器进行调用,我想知道是否可以先从缓存中检查它,如果它存在然后从那里获取它,否则请转到服务器。这怎么可能?这是我当前的代码
I am getting a single entity by using a method fetchEntityByKey
, after that I am loading navigation property for the entity by entityAspect.loadNavigationProperty
. But loadNavigationProperty
always make a call to the server, what I am wondering if I can first check it from cache, if it is exist then get it from there otherwise go the server. How is it possible? Here is my current code
return datacontext.getProjectById(projectId)
.then(function (data) {
vm.project = data;
vm.project.entityAspect.loadNavigationProperty('messages');
});
这里是我封装在 datacontext
服务。
function getProjectById(projectId) {
return manager.fetchEntityByKey('Project', projectId)
.then(querySucceeded, _queryFailed);
function querySucceeded(data) {
return data.entity;
}
}
此外,如何加载导航属性
有一定限制。出于性能原因,我不想一次拥有导航属性的所有记录。
Also, how is it possible to load navigation property
with some limit. I don't want to have all records for navigation property at once for performance reason.
推荐答案
您可以使用EntityQuery.fromEntityNavigation一种基于实体和 navigationProperty 的查询的方法。在这里,您可以通过EntityManager.executeQueryLocally方法在本地执行结果查询。因此,在您的示例中,一旦拥有项目实体,就可以执行以下操作。
You can use the EntityQuery.fromEntityNavigation method to construct a query based on an entity and a navigationProperty . From there you can execute the resulting query locally, via the EntityManager.executeQueryLocally method. So in your example once you have a 'project' entity you can do the following.
var messagesNavProp = project.entityType.getProperty("messages");
var query = EntityQuery.fromEntityNavigation(project, messagesNavProp);
var messages = myEntityManager.executeQueryLocally(query);
您还可以使用EntityQuery.using方法在远程执行和本地执行之间切换查询,就像这样:
You can also make use of the the EntityQuery.using method to toggle a query between remote and local execution, like this:
query = query.using(FetchStrategy.FromLocalCache);
vs
query = query.using(FetchStrategy.FromServer);
这篇关于Breeze-如何从缓存加载导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!