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
|
Dear Mindscape-team, I have a sqlite-database to store local settings. For this local database, I have implemented an error mechanismn to catch the error "not existing local database". Let's assume the local database file exists at program startup and the program can read in the corresponding data. During program run-time entities are modified and these changes shall be written back to the database. However, for some reason, the local database file does not exist anymore. In this case, the task of the error mechanismn shall be to create a new, empty database file. Afterwards all existing entities (independent of entity state) shall be written to this database. And here is my problem: How can I write a collection of entities to an empty database, independent of the entity state of each entity (which will be either default or modified, in this special case)? Thanks for your answer, Henning
|
|
|
You can't do this directly. In LightSpeed 3, UnitOfWork implements IEnumerable<Entity>, so you could iterate over all entities, and "touch" each entity that's in the Default state (set a property to a different value and then set it back again), forcing it to become Modified. In LightSpeed 2, you can kind of simulate this by stashing a reference to each entity during AfterLoad, and iterating over this collection. Note of course that this will save only the items that have been loaded into the unit of work. This may result in save failures e.g. if you have loaded a child item with a FK to a parent, but have not loaded the parent -- the parent will not be saved, so the child's FK will be invalid. You might want to consider an alternative strategy such as locking the SQLite file to prevent deletion, or taking a local copy which you can restore if the real file goes away. |
|
|
Thanks for your fast reply. You wrote an alternative strategy might be locking the SQLite file. Do you know how this works, i.e. locking the SQLite file, and at the same time, still access it with LightSpeed? Regards, Henning |
|
|
I'm thinking do a File.Open with a FileShare setting of FileShare.ReadWrite, so that SQLite can still get at the file to modify it, but it should be locked from deletion. (You wouldn't do anything with the FileStream return from File.Open, other than dispose it when you want to release the lock.) Please note I have not tested this -- e.g. if SQLite requires exclusive access then this wouldn't work. |
|