You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI安卓端SQLite与SQL Server同步失败求助

.NET MAUI中Dotmim.Sync同步SQLite与SQL Server在Android平台报错问题

Windows平台同步功能正常,但Android平台执行同步时出现以下错误:

An error occurred: [InternalEnsureScopeInfoAsync] Overwrite:False.. [InternalGetSchema Async].. [InternalGet TableSchema Async). Table:Product.. A transport level error occurred while receiving results from the server. (provider: TCP Provider, error: 2-Connection terminated)

相关代码

Categories实体类

// Class Categories
public class Categories
{
     [PrimaryKey, AutoIncrement]
     public int Id { get; set; }
     public string NomCategorie { get; set; }
     public string DateCreation { get; set; }
}

同步逻辑代码

// Connection string for SQL Server
string connectionString = @"Data Source=SQL server ;Initial Catalog=db  ;User Id= admin;Password=Password";

// Initialization of Categories
SqlSyncProvider serverProvider = new SqlSyncProvider(connectionString);
string dbPath = Path.Combine(FileSystem.AppDataDirectory, "mydb.db");

SqliteSyncProvider clientProvider = new SqliteSyncProvider(dbPath);

// Configuring synchronization for the "Categories" table
var setup = new SyncSetup("Categories");

// Create the synchronization agent and execute the synchronization
SyncAgent agent = new SyncAgent(clientProvider, serverProvider);
var result = await agent.SynchronizeAsync(setup);

SQL Server表说明

SQL Server中的Categories表包含Id(主键)、NomCategorie、DateCreation三个字段,与客户端实体类结构对应。

解决方案建议

  • 检查Android网络权限:在AndroidManifest.xml中添加必要的网络权限:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    针对Android 6.0及以上版本,需在运行时动态申请网络权限。

  • 验证SQL Server网络可达性:

    • 在SQL Server配置管理器中启用TCP/IP协议,确认监听端口(默认1433)。
    • 开放SQL Server所在服务器的防火墙端口,允许Android设备所在网络的IP访问。
    • 在Android设备上通过telnet工具测试SQL Server的IP和端口,确认网络连通性。
  • 修正连接字符串:

    • 将Data Source替换为SQL Server的实际IP地址和端口,例如Data Source=192.168.1.100,1433。
    • 移除连接字符串中多余的空格,比如User Id= admin改为User Id=admin,确保数据库名称、用户名、密码准确无误。
  • 排查表同步异常:报错中提到了Table:Product,但配置的同步表是Categories,需确认:

    • SQL Server中是否存在Product表,是否被Dotmim.Sync自动检测为关联表。
    • 尝试明确指定同步字段,避免自动检测其他表:
      var setup = new SyncSetup();
      setup.Tables.Add(new SyncTable("Categories") { Columns = { "Id", "NomCategorie", "DateCreation" } });
      
  • 升级Dotmim.Sync版本:检查当前使用的库版本是否支持.NET MAUI Android,尝试升级到最新稳定版,排除版本兼容性问题。

  • 添加详细日志:在同步代码中捕获完整异常信息,便于定位问题:

    try
    {
        var result = await agent.SynchronizeAsync(setup);
        Console.WriteLine($"同步结果:{result}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"完整错误信息:{ex.ToString()}");
    }
    

内容的提问来源于stack exchange,提问作者ALI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 08:50:07