Xamarin sqlite 不能使用 struct 作为 table 数据类型

Xamarin sqlite cannot use struct as table data type

使用sqlite-net-pcl 1.8.116 如果我定义一个结构并从中在数据库中创建一个 table,我无法在没有得到 System.ArgumentException: 'method arguments are incompatible' 的情况下从中读回值。我可以制作 table 并添加行。只有回读失败。

如果我将我的结构变成 class,它可以正常工作。我看了又看是否根本不支持结构,但我没有看到任何迹象表明,甚至其他人也有同样的问题。

我这样创建和使用数据库,失败了

var db = new SQLiteAsyncConnection(path);
db.CreateTableAsync<Flight>().Wait();
var affected = await db.InsertAsync(new Flight());
var get = await db.Table<Flight>().ToListAsync(); //exception

我可以用类似

的sqlQuery做同样的事情,但有同样的例外
var flights = await db.QueryAsync<Flight>("select * from Flight where FlightID==?",id);

这个 post 的精简结构是

public struct Flight
{
  [PrimaryKey, AutoIncrement]
  public int Id { get; set; }
    
  public long Start { get; set; }
  public long End { get; set; }
  public string Description { get; set; }    
}
     

如果我将结构更改为 class,则没有问题。结构是不是像这样不受支持?

是的,我们使用nuget sqlite-net-pcl 1.8.116时就是你说的这种情况。但是你可以尝试使用nuget sqlite-net-pcl的1.7.335版本。当我创建一个 table 并使用 1.7.335 版本插入一些项目时,没有这样的错误。

我发布了一个关于这个问题的新问题,你可以在这里跟进:https://github.com/praeclarum/sqlite-net/issues/1075

感谢您对 xamarin 的反馈。

此致!