New Posts New Posts RSS Feed: Entity manager cache not working
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Entity manager cache not working

 Post Reply Post Reply Page  12>
Author
mgkoko View Drop Down
Newbie
Newbie


Joined: 24-Oct-2012
Posts: 1
Post Options Post Options   Quote mgkoko Quote  Post ReplyReply Direct Link To This Post Topic: Entity manager cache not working
    Posted: 24-Oct-2012 at 3:23am
Hi there,

I've been developing a SPA based on John Papa's example of code camper. I came across with breeze.js and I really want to use this rather than building my own client db context. So far it is OK, but like in code camper example I want to load some data from dataprimer and que the results as needed from local cache. but everytime I tried executeQueryLocally I got 'entityType is null' error. When I check getEntities() to see if I got the results in cached it returns blank but if I check data.results then there are results. my code is

define('dataservice.categories',
    ['jquery', 'underscore', 'ko', 'config', 'utils','breeze', 'store'],
    function ($, _, ko, config, utils, breeze, store) {

        var entityModel = breeze.entityModel,
            core = breeze.core;

        // configure Breeze for Knockout and Web API
        core.config.setProperties({
            trackingImplementation: entityModel.entityTracking_ko,
            remoteAccessImplementation: entityModel.remoteAccess_webApi
        });

        // service name is route to the Web API controller
        var serviceName = '/api/breeze';


        // manager is the service gateway and cache holder
        var manager = new entityModel.EntityManager(serviceName);

        var getData = function () {
            var query = entityModel.EntityQuery.from("Categories");
            manager.executeQuery(query).then(function (data) {
                alert(data.results.length);  //this returns 45 results
                alert(manager.getEntities().length); // this returns 0 results. WHY????
            });
        },
        getByParentUrl = function (url, results) {
            var query = entityModel.EntityQuery.from("Categories");
            query = query.where("Parent.FriendlyUrl", "==", null);
            manager.executeQueryLocally(query); //I think this is the line where I got 'entityType is null' error
        }

        return {
            getData: getData,
            getByParentUrl: getByParentUrl
        }

    });

Please let me know if you have any question
Thank you,
Back to Top
jtraband View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 19-Sep-2012
Posts: 55
Post Options Post Options   Quote jtraband Quote  Post ReplyReply Direct Link To This Post Posted: 24-Oct-2012 at 1:14pm
Ok, I was unable to reproduce the first part of your error (shown below). I do not have a copy of the "CodeCamper" database but I've run the same test with our test db ( a version of Northwind) and manager.getEntities returns an array that it the same length as data.results.length.  So I'm not sure what's going on there.

 var query = entityModel.EntityQuery.from("Categories");
 manager.executeQuery(query).then(function (data) {
        alert(data.results.length);  //this returns 45 results
        alert(manager.getEntities().length); // this returns 0 results. WHY????
 });

As for "executeQueryLocally", it can only execute if the entityManager cache already has data in it.  I cannot tell from your example, but is it possible that you are calling getByParentUrl before any call to getData has completed.   Your getData method is asynchronous whereas your getByParentUrl is not, so you need to make sure that your getData call completes before calling getParentUrl.

If you continue to have issues, can you post a copy of your server side controller?


Back to Top
pawel View Drop Down
Newbie
Newbie
Avatar

Joined: 21-Sep-2012
Posts: 12
Post Options Post Options   Quote pawel Quote  Post ReplyReply Direct Link To This Post Posted: 25-Oct-2012 at 6:53am
As a side note - if you are building a solution based on CodeCamper with Breeze, then remember to remove json camelCasing formatting. Remove the following lines from Alcance.Web.GlobalConfig.CustomizeConfig()
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();

If you leave this configuration Breeze will be unable to populate your objects with data.
Back to Top
svk View Drop Down
Newbie
Newbie


Joined: 11-Oct-2012
Posts: 7
Post Options Post Options   Quote svk Quote  Post ReplyReply Direct Link To This Post Posted: 04-Nov-2012 at 8:04am
Using a very simple test application, I am experiencing the same problem -  manager.getEntities().length always returns 0 while data.results.length returns the actual number of entities.  Below are the components of the test app.  Please let me know if I can provide any more information or if you have any ideas.  Thanks!

Index.chtml looks like this:
<!DOCTYPE html>
<html>
<head>
    <title>Breeze Test</title>
</head>
<body>
Test
<script src="/Scripts/jquery-1.8.2.js"></script>
<script src="/Scripts/knockout-2.2.0.debug.js"></script>
<script src="/Scripts/q.js"></script>
<script src="/Scripts/breeze.debug.js"></script>
<script src="/Scripts/require.js"></script>

<script src="/Scripts/app/main.js"></script>

</body>
</html>

Note: knockout is loaded but not used.  The results are the same even when the test is expanded and a ko array is populated.  The array is filled with entities but manager.getEntities() returns 0 so executeQueryLocally doesn't work.  


main.js boots the app and looks like this:
(function () {

    configRequire();
    boot();

    function configRequire() {
        requirejs.config({
            paths: {'breeze': 'Scripts/breeze.debug'}
        });
    }

    function boot() {

        require(['breeze'], function (breeze) {
            var core = breeze.core,
                entityModel = breeze.entityModel;

            core.config.setProperties({
                trackingImplementation: entityModel.entityTracking_ko,
                remoteAccessImplementation: entityModel.remoteAccess_webApi
            });

            var serviceName = 'api/breezetest';
            var manager = new entityModel.EntityManager(serviceName);

            var query = new entityModel.EntityQuery().from("Items");

            manager.executeQuery(query)
                .then(
                    function (data) {
                        alert("Got " + data.results.length + " data rows");
                        alert("Got " + manager.getEntities().length + " entities");
                    })
        });

    }
})();

controller:
namespace BreezeTest.Controllers
{
    public class BreezeTestController : ApiController
    {
        readonly EFContextProvider<BreezeTestDbContext> _contextProvider = new EFContextProvider<BreezeTestDbContext>();

        [AcceptVerbs("GET"), ActionName("Metadata")]
        public string Metadata()
        {
            return _contextProvider.Metadata();
        }

        [AcceptVerbs("GET"), ActionName("Items")]
        public IQueryable<Item> Items()
        {
            return _contextProvider.Context.Items;
        }
    }
}

metadata:
{"conceptualModels":{"schema":{"namespace":"BreezeTest.Data","alias":"Self","d4p1:UseStrongSpatialTypes":"false","xmlns:d4p1":"http://schemas.microsoft.com/ado/2009/02/edm/annotation","xmlns":"http://schemas.microsoft.com/ado/2009/11/edm","entityType":{"name":"Item","key":{"propertyRef":{"name":"Id"}},"property":[{"name":"Id","type":"Edm.Int32","nullable":"false","d4p1:StoreGeneratedPattern":"Identity"},{"name":"Name","type":"Edm.String","fixedLength":"false","maxLength":"Max","unicode":"true","nullable":"true"},{"name":"Description","type":"Edm.String","fixedLength":"false","maxLength":"Max","unicode":"true","nullable":"true"}]},"entityContainer":{"name":"BreezeTestDbContext","entitySet":{"name":"Items","entityType":"Self.Item"}}}}}

model:
namespace BreezeTest.Models
{
    public class Item
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

    }
}

dbcontext:
namespace BreezeTest.Data
{
    public class BreezeTestDbContext : DbContext
    {
        public DbSet<Item> Items { get; set; }
    }
}
Back to Top
svk View Drop Down
Newbie
Newbie


Joined: 11-Oct-2012
Posts: 7
Post Options Post Options   Quote svk Quote  Post ReplyReply Direct Link To This Post Posted: 06-Nov-2012 at 8:57am
Any thoughts?  Am I doing something incorrectly or might I be running up against a bug?  Thanks.
Back to Top
jtraband View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 19-Sep-2012
Posts: 55
Post Options Post Options   Quote jtraband Quote  Post ReplyReply Direct Link To This Post Posted: 06-Nov-2012 at 10:36am
Looking at it now.
Back to Top
CCPony View Drop Down
Newbie
Newbie


Joined: 07-Nov-2012
Location: Maryland
Posts: 3
Post Options Post Options   Quote CCPony Quote  Post ReplyReply Direct Link To This Post Posted: 07-Nov-2012 at 9:35am
I'm currently experiencing this exact same problem.

Edited by CCPony - 07-Nov-2012 at 9:37am
Back to Top
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Posted: 07-Nov-2012 at 9:57am
Hang in there. We'll take SVK's sample and try to repro. With luck everything we need will be there ... or at least we'll have a shared solution to kick around.
Back to Top
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Posted: 07-Nov-2012 at 11:28am
I have a test bed that matches SVK's sample ... except it doesn't yet use RequireJS. It works fine. Will now create a second version that uses Require. Maybe that is introducing a problem ... although we use Require all over the place.
Back to Top
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Posted: 07-Nov-2012 at 12:02pm
Ok everyone ... I cannot reproduce your issues. But I can give you two solutions, one with Require and one without, that follow the path prescribed by svk. I've attached both.

If you want to press this issue productively, I suggest that you use either of these solutions as a baseline for your reproduction of the error.
Back to Top
CCPony View Drop Down
Newbie
Newbie


Joined: 07-Nov-2012
Location: Maryland
Posts: 3
Post Options Post Options   Quote CCPony Quote  Post ReplyReply Direct Link To This Post Posted: 07-Nov-2012 at 12:21pm
I'm new around here. Ward, I don't see your attachments. Where are they? Thanks.
Back to Top
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Posted: 07-Nov-2012 at 12:29pm
I'm old around here and I don't see them either. Some problem with the forum attachment mechanism. 

For now, you can get them at

Until we get forum attachment straightened out, you'll have to tell us where to get the repro solutions that you create.
Back to Top
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Posted: 07-Nov-2012 at 2:10pm
Now that I know how to attach a file, I present you with these same zip files attached to the forum post itself.


If you can reproduce the problem using either of these solutions and want us to evaluate it, you can upload your solution as I did here..

Back to Top
svk View Drop Down
Newbie
Newbie


Joined: 11-Oct-2012
Posts: 7
Post Options Post Options   Quote svk Quote  Post ReplyReply Direct Link To This Post Posted: 07-Nov-2012 at 2:56pm
Thanks for the sample projects, Ward.  I was able to identify the problem.  Simply put, Breeze requires that the models and dbcontext be in the same namespace. 

You can repro the described behavior in your projects by moving BreezeTestDbContext & BreezeTestDatabaseInitializer into a different namespace. 

Is this something that can easily be fixed?  Thanks!
Back to Top
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Posted: 07-Nov-2012 at 3:13pm
Yes, that is a known and documented requirement. The Model classes and the DbContext(s) must be in the same namespace;  the controller can be in a different namespace. 

We won't be trying to "fix" this any time soon. The Entity Framework tooling we use to generate the metadata only supports a single shared namespace and we don't think building our own metadata generation is a good investment at this time.

This issue ... and a workaround ... are discussed in our documentation on using the Entity Framework DbContext in a Breeze application.

After you've looked at that, if you still feel this is a critical shortcoming, please let us know and explain why.
Back to Top
CCPony View Drop Down
Newbie
Newbie


Joined: 07-Nov-2012
Location: Maryland
Posts: 3
Post Options Post Options   Quote CCPony Quote  Post ReplyReply Direct Link To This Post Posted: 07-Nov-2012 at 5:44pm
I did some testing just came to the same conclusion. Thanks for handling so quickly.

Edited by CCPony - 07-Nov-2012 at 6:04pm
Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down