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 have to copy entities from the server to the client database (the ids have to be the same). Therefor i have to contexts, one dbServerContext and one dbClientContext. using (var uow_server = dbServerContext.CreateUnitOfWork()) // if it doesn't exist on the client, i will create/copy it // this causes the exception: Expected EntityState.New - Parametername: entity // the EntityState is default, manually setting to new is not allowed uow_client.Add(newEntity );
I have nothing found in your forum. So do you have an idea?
Best regards Markus |
|
|
As you might already thought // if it doesn't exist on the client, i will create/copy it this should be // if it doesn't exist on the client, i will create/copy it
I played a bit more: newEntity = server_myEntity; If i "touch" the Entity, the state is set to Modified - but when saved, nothing happens. If i could set the state to new, i think it would be saved correctly. But the Entity state could not be changed. I use the current beta 3. |
|
|
This isn't easy to do with LightSpeed: in general we'd recommend looking at an alternative, more database-level approach, such as the Microsoft Sync Framework or a database replication utility. If you do use LightSpeed, you face a bit of a fight because LightSpeed assumes that it is in control of entity IDs and the entity lifecycle. So if you want to put new entities into the client database, LightSpeed is going to want to assign them IDs; but if you try to attach Modified entities to the client UOW, LightSpeed is going to assume it needs to do an UPDATE rather than an INSERT, and of course the UPDATE won't work because there's no matching database entry to be updated. So there are two things you need to do: 1. Take control of the ID by overriding GeneratedId(). Your implementation will normally call base.GeneratedId(), but in the presence of a magic "I'm being synchronised" indicator it would return the ID cloned from the server entity. 2. Get the data from the server entity into the client entity. There are two ways of doing this: 2a. Create a new entity for the client, copy the properties from the server entity, and add the new entity to the client UOW. This will result in an INSERT but requires you to write and maintain a copy routine. 2b. Create a new entity with empty or dummy data, add it to the client UOW, and do a SaveChanges. This will perform an INSERT into the database. Now attach the server entity to the client UOW (which will effectively replace the dummy entity in the identity map) and touch it to get it into the Modified state. Do another SaveChanges. This will perform an UPDATE which should now work because there is a row in the database with the correct ID. This is very kludgey and less efficient but saves you writing and maintaining a copy routine. You may be able to bypass the manual copy routine by using something like AutoMapper, in which case 2a is definitely the way to go. I'd recommend 2b only if the copy routine is likely to be very complicated or to change frequently. But to reiterate, any LightSpeed-based solution for database sync is likely to be a bit messy. We'd encourage you to look at something like the Sync Framework, or to relax the requirement that IDs must be the same across client and server, if that's at all an option. |
|
|
Hi Ivan, I've dug up this old post because I was wondering about the usage of AutoMapper with LightSpeed. With my previous post regarding working with entity from ASP.NET webform, I thought I'd give the "retain ID then reload on postback" approach a go. With this, I would have to write quite a bit of mapping code. This is when I thought about messing around with DTO and AutoMapper. Is this the right way to go about ASP.NET webform development with LightSpeed? I think I also saw something from one of the Mindscape guy mentioning that you don't need AutoMapper as DTO does that. Would appreciate your thoughts! Thanks Joseph
|
|
|
I don't know enough about AutoMapper to know whether it could handle mapping between a collection of ASP.NET Web Form controls and a LightSpeed entity. But if it can, then that sounds like it would be an efficient and elegant solution. If you do give this a go, I'd be very interested to hear the results. I don't recall the comment about not needing AutoMapper because of DTOs, but I think it must have referred to the fact that the designer generates extension methods for mapping entities to designer-generated DTOs and vice versa. These methods are specific to DTOs and wouldn't help with the Web Form control mapping scenario. |
|
|
Thanks Ivan, really appreciate your thoughts. |
|