本地数据缓存 - 如何强制与数据库完全同步?

发布于 2024-08-09 01:23:09 字数 389 浏览 2 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜唯美灬不弃 2024-08-16 01:23:09

正如我在评论中指出的,我不确定为什么您的本地数据库会如此频繁地被覆盖,但如果它确实被覆盖,并且不重新同步之前发送到服务器的更改,那么问题是最严重的锚点可能存储在服务器上。即使客户端数据库已“回滚”到以前的版本,服务器也不会意识到这一点,并且确实无法发现。

当然,如何实际实现同步和锚定逻辑完全取决于个人,所以我只能猜测问题的具体情况,但为了解决它,您需要做一个有两件事:

  • 拥有一个在服务器上运行的脚本,用于重置您使用的开发客户端 ID 的客户端锚点;或者
  • 将项目中 SDF 的构建复制属性(“复制到输出目录”)更改为“不复制”,以便本地数据库永远不会被覆盖。

希望有帮助。

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:

  • Have a script that you run on the server that resets the client anchor for the development Client ID that you use; or
  • Change the build copy properties ("Copy To Output Directory") of the SDF in your project to "Do Not Copy", so that the local database never gets overwritten.

Hope that helps.

素手挽清风 2024-08-16 01:23:09

如果 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...

童话里做英雄 2024-08-16 01:23:09

我在 Microsoft 论坛中得到的答案可能会解决您的问题。

如果您通过将主键设置为自身来更新相关表,则记录的版本将增加,并且它们将包含在下一次同步中。您可以使用类似于以下的 SQL 语句来执行此操作。

update MyTable set Id = Id;

请注意,您的场景存在一些差异。

  1. 您将增加版本,而不是像我的情况那样初始化它。
  2. 您将在服务器上而不是在精简版数据库中操作更改跟踪。

未经测试,我认为该解决方案应该适合您 - 让我们知道您的进展如何!

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.

update MyTable set Id = Id;

Note that there are a couple of differences in your scenario.

  1. You will be incrementing the version and not initializing it as in my case.
  2. You will be manipulating change tracking on the server and not in the Compact Edition database.

Without having tested it, I think that this solution should work for you - let us know how you get on!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文