Search code examples
javascriptentitywcf-data-servicesodatajaydata

JayData create object not working as expected


I find it very hard to find any info about this so I'm asking it on SO.

I want to create a new "Connection" with the JayData Entity API. A Connection has 2 DevicePorts, both linked with a Device and a Port. So:

Connection(Sender: DevicePort(Device, Port), Receiver: DevicePort(Device, Port))

I already have 2 DevicePorts (ids: 1 and 2)

I want to create a new Connection with those as sender and receiver. So I do this:

var devPort1 = previouslyFetchedDevicePortEntity1;
var devPort2 = previouslyFetchedDevicePortEntity2;
var con = DataLayer.context.ConnectionSet.add({ Sender: devPort1, Receiver: devPort2 });
DataLayer.context.saveChanges();

This is working and a new Connection is added to the database. But there are also new DevicePorts, new Devices and new Ports. It seems like its cloning the previous DevicePorts etc, but I want a new Connection with the same DevicePorts I gave it as parameter.


Solution

  • I think the following 2 lines are missing to initialize the entitySet of the previously loaded items:

    DataLayer.context.Ports.attach(devPort1);
    DataLayer.context.Ports.attach(devPort2);
    

    I just assume that you have an EntitySet for the Port entity, substitute the name of your set here.

    Does this solve the problem?