0

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.

HansElsen
  • 1,639
  • 5
  • 27
  • 47

1 Answers1

2

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?

Robesz
  • 1,646
  • 11
  • 13
  • Hi thanks! This works. It's now necessary to attach the devPorts as well as the devices and ports of the devPorts. Like this: `DataLayer.context.DevicePortSet.attach(sender); DataLayer.context.DevicePortSet.attach(receiver); DataLayer.context.DeviceSet.attach(sender.Device); DataLayer.context.DeviceSet.attach(receiver.Device); DataLayer.context.PortSet.attach(sender.Port); DataLayer.context.PortSet.attach(receiver.Port);` Could this be made more efficient/clean? – HansElsen Feb 25 '13 at 12:21
  • nevermind I saw that I included the ports and devices when I fetched the devPorts. That way I had to attach them when I used them in a new connection. – HansElsen Feb 25 '13 at 12:32