This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hello - I know how to do this in a stored procedure, but I want to do it using the LightSpeed tools. I have a master record with several child records. The master record will read values from some entity which only changes occasionally, so I'd like to use the following logic to save the child record on Add: If the child record does not exist, add as a new record If the child record does exist, set the FK field to the ID of the existing record. Save the master record.
How do I do this? I think, if I can figure this out, I will have quite a bit more insight into how to manipulate the generated code to do what I need it to do... Thanks, Dave |
|
|
I'm not sure I've quite understood your scenario. It sounds like you're creating an entity of type Master, which has a collection Children of type Child, and you want to be able to do the following: * Create a new Child and assign it to this Master. * Take an existing Child and assign it to this Master. Then when you save the Master, you want new Child entities to be INSERTed and existing Child entities to be UPDATEd. Is that correct? If so, LightSpeed does this automatically when you wire up the Child to the Master, whether the Child is an existing Child or a newly created one. Thus, setting existingChild.Parent = newMaster (or writing newMaster.Children.Add(existingChild)) will result in existingChild's ParentId being updated and saved. If that's not your scenario, could you clarify what you're trying to do? Thanks! |
|
|
Wow, I really need to stop writing help requests right before I take off for the day. The time zone difference had me thinking I needed to send it off in a hurry, and I made quite a few blunders -- please accept my apologies, and here's the real scenario... |
|
|
You need to basically do what you're doing at the moment, except that you shouldn't create and wire up a new Ref object unless you need it. At the moment you ALWAYS create and wire up a new Ref object, and only later do you decide that you don't need it, by which time it's too late -- the new one is already in the UOW, and is doomed to being saved as an INSERT. So all you need to do is move that code path to inside the if-else so it only runs if there are no suitable Refs existing: Master entity = new Master(); Incidentally, as a minor optimisation, you can probably save a database query by writing: var existingRef = uow.Masters.Where(...).FirstOrDefault(); This saves you the Count() query. Also note there are possible race conditions here (if Threads A and B check the database around the same time, both will go down the "create new" path). |
|
|
Thanks, Ivan! That did the trick. I wondered about that possibility, but didn't try it for some reason. Also, thanks for the tip re the Count() function. I didn't think about the fact that would generate more trips to the database, but looking back at it I can understand why it would. Still struggling with Through Associations, but getting closer to having what I need to figured out.
Dave |
|