If I understand your question, is that what you are trying to do?
using (new TransactionScope()) { Manifest manifest = new Manifest { AllocatedTransactions.Add(new AllocatedTransaction { Quantity = 5 } }; DataContext.Manifests.InsertOnSubmit(manifest); DataContext.SubmitChanges(); Manifest newManifest = DataContext.Manifests.Where(a => a.ID == manifest.ID).SingleOrDefault(); Assert.AreEqual(manifest.AllocatedTransactions.First().Quantity, newManifest.AllocatedTransactions.First().Quantity); }
You also do not need to manually retrieve the AllocatedTransaction objects associated with the manifest. Just retrieve the Manifest object, as I did with the newManifest object, and all associated AllocatedTransaction objects should follow.
UPDATE:
It looks like you are trying to connect in the wrong direction. You need to bind the selected Transact to the manifest, and not vice versa:
Manifest manifest = DataContext.Manifests.Single(a => a.ID == 27); AllocatedTransaction allTrans = DataContext.AllocatedTransactions.Single(a => a.ID == 53); manifest.AllocatedTransactions.Add(allTrans); DataContext.SubmitChanges();
This assumes that the Manifest and AllocatedTransaction records already exist. I highly recommend you ** not * pre-populate the ManifestID field in the AllocatedTransactions object. If you add it, as I showed above, the LINQ mechanism will automatically allow and update the value of the foreign key. If you set the value ahead of time, it may reset the ChangeSet and assume that you are trying to add a new record, rather than join an existing one.
source share