Sql server CE 4.0 in a Visual studio project suddenly losing data
I am not sure if this is a setup issue from my side or some bug, but I am facing a really wierd problem.
I am developing against SQL Server CE 4.0 in a VS 2010 Winforms Project. It is kept in the project like so
Now what happens is, for some strange reason, whenever I encounter an exception in my application or do a schema update for database, I lose all my test data. Not just one table, all of it, entire database except for the schema objects.
Remember, I am not doing anything to regenerate the database from the model or anything. It just deletes all the data, 开发者_运维问答and currently it is not even fully reproducible. Completely unpredictable. The schema remains intact even after the data is deleted.
Can any one help?
Okay I figured out the solution - very stupid of me, but I think others can make this same mistake so here goes -
I am using the connection string as |DataDirectory|\LocalDB.sdf and expected it to take it from the project directory - turns out it takes it from the debug folder instead. Makes sense now that I know it.
So whenever I do a schema update, the original database gets changed so it replaced the LocalDB present in the debug folder. Since the db in the project never had any data, I used to get the empty database unnecessarily.
Actually, after a change in the SQL Server CE database (in the schema, for example), it replaces the database inside the application with a new copy. If the database in sqlserver ce has a different set of data than the database inside the application, the previous data inside the application will be lost.
精彩评论