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
|
I am using LS 4, to copy data from one database to another. I have a small class / method that connects to both databases, it gets a list The problem is, it is not reusing the existing ID in the new table, it is creating a new one. This is causing me relationship problems. Is there a way for the Import() method to reuse the existing ID. My code is as follows:
|
|
|
No, Import will either update the existing copy of the entity or add the entity as new (which generates a new Id). Neither Import or Attach directly support this but you can trick LightSpeed with Attach into believing the entity is new by assigning the EntityState and then saving it with the already assigned Id. Here is a quick code example of what you will need to do:
|
|
|
Hi Jeremy, Many thanks for the advice. Unfortunately I think the workaround you suggested would not work. The reason being, the Source type and the Destination type are slightly different, depending on direction of data flow, either the source or destination table holds only a subset of the fields in the other type. This is why I was trying to use the Import option. Is there anyway with the import option that we can use the GenerateId override to set the ID of a newly created entity? All of our entities are defined with a ManualID field, tied to the GenerateId() method. See code below.
I can think of one workaround to manually create an entity and use the GenerateId override to set the ID, save it, then import the source, and as it will see the existing entity it will update it. But this seems so messy and resource intensive. Especially as we need to run these routines on many tables, at a high frequency, i.e. every table once every couple of minutes. Any other suggestions you may have would be greatly appreciated. Best regards Mark |
|
|
Based on your feedback, and my ideas of using some workarounds I have come up with this code. This seems to work, but is slow, I guess because of the reflection? Can you think of any ways to optimise this code to make it faster?
|
|
|
I would say this will be primarily slow because of this:
This will be fetching everything in that table because the All will have to be resolved client side due to the method calls. Try something like this instead:
The reflection cost shouldn't be significant compared to the cost of database queries :)
|
|