Search code examples
c#javascriptbreezedurandal-2.0

Breeze entity Key not set at creation time


I have the following JS script that creates a product object:

var entityQuery = breeze.EntityQuery,
        manager = configureBreezeManager();
function configureBreezeManager() {
    breeze.NamingConvention.camelCase.setAsDefault();
    return new breeze.EntityManager(config.remoteServiceName);;
}
...
var createProduct = function () {
    var product = manager.createEntity('Product', { name: '', description: ''});
    return product;
};

and the server back end:

public class Product{
    int ProductId{get;set;}
    String Name{get;set;}
    String Description{get;set;}
}

The problem is that the product's key is always -1 when created. Trying to a product locally by id (-1) returns an empty result.

How can I create an entity and be able to get it back when needed (without first saving the entity to the server)?


Solution

  • Not entirely sure I understand the question. But when you call EntityManager.createEntity the entity is created and added to the EntityManager's cache.

    This means that when you call EntityManager.getEntities you will find the entity, or when you run a local cache query via EntityManager.executeQueryLocally or query with a FetchStrategy of FetchStrategy.FromLocalCache you will also find the entity.

    The entity will not appear on the server until you perform a save at which time the local entity's will change from a temporary key to a permanent one. For more see the "Temporary Key Generation" topic on this page: http://www.breezejs.com/documentation/extending-entities