0

I am trying to add the entity which is related to another entity

    onlinedb.DemoShifts.filter("it.EmployeeID ==="+empID).toArray(function (DemoShift) {

        DemoShift.forEach(function (demoShift) {

            offlinedb.DemoShifts.add(demoShift);
            alert("Add DemoShift");
            onlinedb.Sites.filter("it.SiteID==="+demoShift.SiteID).toArray(function(Sitess){
                Sitess.forEach(function(site){

                    onlinedb.SiteChains.filter("it.ChainID==="+site.ChainID).toArray(function (chains) {
                        offlinedb.attach(chains[0]);
                        alert("Add SiteChain");
                    });

                    offlinedb.add(site);
                    var res = offlinedb.saveChanges();
                    res.done(function () { alert("Success"); });
                    res.fail(function (ex) { alert("Success"); });
                });
            });

        });

    });

I am getting this error "Context already contains the entity"

What is exactly wrong I am doing Can you tell me is there any way I can check if the entity already exists or something?

1 Answers1

0

The library throws this error if the entity is attached more than one. What happens if you comment the following line?

offlinedb.attach(chains[0]);
Robesz
  • 1,646
  • 11
  • 13