OpusSolution.Tasken.Sync.Core 1.0.0-development

This is a prerelease version of OpusSolution.Tasken.Sync.Core.
dotnet add package OpusSolution.Tasken.Sync.Core --version 1.0.0-development
                    
NuGet\Install-Package OpusSolution.Tasken.Sync.Core -Version 1.0.0-development
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpusSolution.Tasken.Sync.Core" Version="1.0.0-development" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpusSolution.Tasken.Sync.Core" Version="1.0.0-development" />
                    
Directory.Packages.props
<PackageReference Include="OpusSolution.Tasken.Sync.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OpusSolution.Tasken.Sync.Core --version 1.0.0-development
                    
#r "nuget: OpusSolution.Tasken.Sync.Core, 1.0.0-development"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package OpusSolution.Tasken.Sync.Core@1.0.0-development
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OpusSolution.Tasken.Sync.Core&version=1.0.0-development&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=OpusSolution.Tasken.Sync.Core&version=1.0.0-development&prerelease
                    
Install as a Cake Tool

OPUS.Tasken.Sync.Core

Phiên bản: Development 1.0.0

OPUS.Tasken.Sync.Core là thư viện .NET 8 dùng để đồng bộ dữ liệu từ database master SQL Server/Azure SQL sang một hoặc nhiều database đích. Package sử dụng SQL Change Tracking, checkpoint riêng theo từng bảng, metadata của bảng và generic SQL writer để xử lý đồng bộ incremental.

Package này chỉ chứa sync engine dùng lại được. Nó không tự chạy lịch. Host bên ngoài, ví dụ OPUS.Tasken.Functions.Sync, chịu trách nhiệm trigger theo lịch và gọi sync runner.

Chức năng chính

  • Đồng bộ incremental dựa trên SQL Change Tracking.
  • Hỗ trợ mô hình 1 database nguồn sang 1 hoặc nhiều database đích.
  • Cấu hình whitelist bảng được phép sync.
  • Checkpoint riêng theo JobName + TargetName + TableName.
  • Source reader và target writer dạng generic dựa trên metadata.
  • Xử lý insert, update và delete.
  • Hỗ trợ bảng có identity column bằng SET IDENTITY_INSERT.
  • Xử lý theo batch.
  • Đăng ký DI qua AddTaskenSyncCore.
  • Kèm database scripts để tạo bảng control và bật Change Tracking.

Trạng thái package

Mục Giá trị
Package OpusSolution.Tasken.Sync.Core
Phiên bản Development 1.0.0
Target framework .NET 8
Database SQL Server / Azure SQL
Cơ chế sync SQL Change Tracking
Host chạy lịch Cần host bên ngoài, ví dụ Azure Function

Cài đặt

Sau khi package được publish lên NuGet:

dotnet add package OpusSolution.Tasken.Sync.Core --version 1.0.0

Cách sử dụng cơ bản

Đăng ký Sync Core trong host .NET:

using OPUS.Tasken.Sync.Core.Extensions;

builder.Services.AddTaskenSyncCore(builder.Configuration);

Inject và chạy job:

using OPUS.Tasken.Sync.Core.Abstractions;

public sealed class SyncWorker
{
    private readonly ISyncJobRunner _runner;

    public SyncWorker(ISyncJobRunner runner)
    {
        _runner = runner;
    }

    public async Task RunAsync(CancellationToken cancellationToken)
    {
        await _runner.RunAsync("MasterToTasken", cancellationToken);
    }
}

Cấu hình

Ví dụ cấu hình:

{
  "ConnectionStrings": {
    "MasterDb": "<connection-string-database-nguon>",
    "TargetDb": "<connection-string-database-dich>"
  },
  "Sync": {
    "Jobs": [
      {
        "Name": "MasterToTasken",
        "SourceConnectionStringName": "MasterDb",
        "Tables": [
          "AspNetUsers",
          "Department",
          "Request",
          "YearlyTarget"
        ],
        "Targets": [
          {
            "Name": "TaskenTargetDb",
            "ConnectionStringName": "TargetDb",
            "IsEnabled": true,
            "BatchSize": 500
          }
        ]
      }
    ]
  }
}

Tables là danh sách whitelist. Sync Core không tự động đồng bộ toàn bộ database.

Chuẩn bị database

Chạy các script trong thư mục database-script trên database master:

Thứ tự Script Mục đích
1 001-create-sync-control-tables.sql Tạo schema sync và các bảng control
2 002-enable-change-tracking-for-sync-tables.sql Bật SQL Change Tracking cho database và các bảng cấu hình
3 003-initialize-sync-checkpoints.sql Khởi tạo baseline checkpoint

Chỉ chạy script baseline checkpoint sau khi database đích đã có dữ liệu nền tương ứng. Nếu database đích đang trống, cần seed dữ liệu ban đầu trước.

Luồng xử lý

Sync job
  -> target đang enabled
    -> bảng trong cấu hình
      -> đọc checkpoint
      -> kiểm tra Change Tracking min valid version
      -> đọc changes từ source
      -> đọc source rows cho insert/update
      -> ghi transaction vào target
      -> update checkpoint sau khi thành công

Interface chính

Interface Trách nhiệm
ISyncJobRunner Điều phối job, target, bảng và batch
ISyncSourceReader Đọc Change Tracking version và changed rows
ISyncTargetWriter Ghi insert, update và delete vào target
ISyncCheckpointStore Đọc và cập nhật checkpoint
ITableMetadataProvider Đọc primary key, identity column và writable columns
ISyncRunRecorder Ghi nhận kết quả sync qua logging hoặc storage

Bảng control

Implementation hiện tại dùng checkpoint trong database master:

sync.SyncCheckpoints

Database script cũng tạo thêm các bảng control phục vụ mở rộng:

sync.SyncJobs
sync.SyncTargets
sync.SyncJobTargets
sync.SyncRuns

Lỗi thường gặp

Lỗi Nguyên nhân Cách xử lý
Invalid object name 'sync.SyncCheckpoints' Chưa tạo bảng control Chạy 001-create-sync-control-tables.sql
Checkpoint version ... older than Change Tracking min valid version Checkpoint quá cũ hoặc chưa baseline Seed target, sau đó chạy 003-initialize-sync-checkpoints.sql
Table 'dbo.X' was not found Sai tên bảng hoặc bảng không tồn tại ở source Sửa Sync:Jobs:Tables hoặc tạo bảng đúng schema
Change Tracking is not enabled for table Bảng chưa bật Change Tracking Chạy 002-enable-change-tracking-for-sync-tables.sql
Ghi target lỗi Target lệch schema hoặc thiếu quyền Đồng bộ schema và cấp quyền phù hợp

Ghi chú vận hành

  • SQL Change Tracking chỉ tracking thay đổi dữ liệu theo row, không tracking thay đổi schema.
  • Schema target phải tương thích với source.
  • Mỗi bảng cần primary key để generic sync hoạt động.
  • Identity value được giữ nguyên từ source sang target.
  • Không lưu secret trong repository. Nên dùng environment variables, Azure Function App Settings hoặc Azure Key Vault.

Tài liệu liên quan

  • docs/sync-core/00-Index.md
  • docs/database-master-sync/03-sync-core-and-function-usage-guide.md
  • database-script/001-create-sync-control-tables.sql
  • database-script/002-enable-change-tracking-for-sync-tables.sql
  • database-script/003-initialize-sync-checkpoints.sql
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-development 81 6/29/2026

Development 1.0.0