本地数据缓存 - 如何强制与数据库完全同步?
我的 asp.net 3.5 应用程序中有一个本地数据缓存。我注意到,有时,特别是在开发过程中,我的本地数据库会与服务器不同步。我明白为什么,我可以通过以下步骤重现它:
1- Start the app
2- Make a change
3- Sync changes with server
4- Start the app again, thus forcing the app to use a new copy of
the local .SDF file.
我之前所做的更改显然消失了,但是当我与服务器同步时,它们没有被拉下来。我的猜测是应用程序在某个地方没有检测到数据库不同步。
我想知道是否有一种方法可以以编程方式告诉我的应用程序与服务器进行完全同步。有人知道吗?
I've got a Local Data Cache in my asp.net 3.5 app. I've noticed that once in a while, especially while developing that my local database will get out of sync with the server. I understand why and I can reproduce it with these steps:
1- Start the app
2- Make a change
3- Sync changes with server
4- Start the app again, thus forcing the app to use a new copy of
the local .SDF file.
The changes I made previously are gone obviously, but when I sync with the server, they are not pulled down. My guess is that somewhere the app isn't detecting that the DB is out of sync.
What I'm wonder is if there is a way to programmatically tell my app to do a full sync with the server. Anyone know?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如我在评论中指出的,我不确定为什么您的本地数据库会如此频繁地被覆盖,但如果它确实被覆盖,并且不重新同步之前发送到服务器的更改,那么问题是最严重的锚点可能存储在服务器上。即使客户端数据库已“回滚”到以前的版本,服务器也不会意识到这一点,并且确实无法发现。
当然,如何实际实现同步和锚定逻辑完全取决于个人,所以我只能猜测问题的具体情况,但为了解决它,您需要做一个有两件事:
希望有帮助。
As I noted in the comment, I'm not sure why your local database would be getting overwritten so frequently, but if it does get overwritten, and does not re-sync changes that were previously sent to the server, then the problem is most likely that the anchors are stored on the server. Even though the client database has been "rolled back" to a previous version, the server is not aware of this and really has no way to find out.
Of course it's entirely up to the individual how to actually implement the sync and anchor logic, so I can only guess at the specifics of the problem, but in order solve it, you'll need to do one of two things:
Hope that helps.
如果 SQL Services 中的跟踪更改间隔选项太小,您也会错过更新。
例如,如果设置为 2 天,则发生更改,然后在尝试同步之前有 3 天,您将错过这些更改。
在通过如上所述更改记录来确保进行更新的同时,我发现对我来说最好的方法是将每个同步表的上次同步日期存储在单独的同步表中,并每次都完整下载该表。
然后允许同步发生。
然后,对于同步未更新的每个表,检查日期是否早于跟踪更改间隔。如果是,请更改该表的同步选项以完成下载并再次同步。
有点浮夸,我知道...
IF the Track Changes Interval option in SQL Services is too small you will also miss updates.
e.g. if its set for 2 days, then a changes happens, then theres 3 days before a sync attempt, you will miss those changes.
While making sure theres an update by changes a record as above, I've found that the best method for me is to store the date last synced for each sync table in a seperate sync table and have that table downloaded completely each time.
Then allow the sync to happen.
Then, for each table that wasnt updated by the sync, check to see if the date is more than the Track Changes interval ago. If it is, change the sync option for that table to complete download and sync again.
A bit faffy, I know...
我在 Microsoft 论坛中得到的答案可能会解决您的问题。
如果您通过将主键设置为自身来更新相关表,则记录的版本将增加,并且它们将包含在下一次同步中。您可以使用类似于以下的 SQL 语句来执行此操作。
请注意,您的场景存在一些差异。
未经测试,我认为该解决方案应该适合您 - 让我们知道您的进展如何!
An answer that I got in the Microsoft Forums may provide a solution to your problem.
If you update the tables in question by setting the primary key to itself, the versions of the records will be incremented and they will be included in the next synchronization. You could use an SQL statement similar to the following to do this.
Note that there are a couple of differences in your scenario.
Without having tested it, I think that this solution should work for you - let us know how you get on!