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'm having trouble getting changes made to an entity object to save to my SQLite database. I have a class (let's call it ClassA for simplicity) with X number of properties. Each property value is changed using the Entity.Set method so Lightspeed can keep track of what needs to be saved and what doesn't. When I make changes to an object of type ClassA (called ObjectA) from a UnitOfWork inside of ClassB (e.g. ObjectA.Title = "something new") the EntityState does not change. Therefore, the new object properties are not saved to the database. Am I missing something in terms of the usage of Lightspeed? Here's some source code to kind of explain what I mean.... ------------------- public class ClassA : Entity<int> { [Indexed] private string m_title; public string title { get { return m_title; } set { Set(ref m_title, value); } } } ------------------- public class ClassB { ... private LightSpeedContext context; ... public ClassB() { context = new LightSpeedContext(); string dbPath = "C:\\test.sqlite"; context.ConnectionString = "server=localhost; Data source=" + dbPath + "; Integrated Security=SSPI;"; context.DataProvider = DataProvider.SQLite3; context.IdentityMethod = IdentityMethod.IdentityColumn; } ... public void saveChanges(ClassA instance) { using (IUnitOfWork uow = context.CreateUnitOfWork()) { ClassA objectA = uow.FindById<ClassA>(instance.Id); if (objectA != null) { // the object actually exists in the DB objectA.title = instance.title; uow.SaveChanges(); } } } ... } ------------------- |
|
|
Your code looks fine and should result in an UPDATE statement. The only things I can think of are: * objectA is null, i.e. the FindById is not succeeding. That could happen if instance.Id is not what you are expecting. * objectA.title already has the same value as instance.title. If this happens, then LightSpeed will detect that the title has not changed and will not mark the entity as modified (and will therefore not send an UPDATE statement). You can also try setting context.Logger = new TraceLogger() to see whether LightSpeed is sending an UPDATE statement. If you can rule out these two scenarios then could you provide us with a console or NUnit test project (including the SQLite database file) that demonstrates the issue please, and we will take a look for you. Thanks! |
|
|
Hi Ivan, I tested to make sure that objectA is not null and that the new properties being assigned to it do not match its current values. Neither of these seemed to work. I stripped the code from my project and threw it into a smaller console project for you to view. Strangely enough, it works just fine in the console project. I added a logger to the context so that I can view what's going on behind the scenes and I see both the SELECT and UPDATE statements being executed as they should. My only guess now is that it has something to do with the multi-threaded nature of my main project. I'm almost positive that the thread that executes the saveChanges method is not the same thread that created the LightSpeedContext object. I don't see why that should be an issue, but you never know... -Chris |
|
|
Hi Ivan, Figured it out. It was a naming problem. I originally had my private ClassA members named according to the convention "m_property", which were accessed using public properties named according to the convention "property". To fix the update problem I changed my private member naming convention to "property" and the public properties naming convention to "Property". Everything is working great now! Thanks for your help :-) -Chris |
|