Xamarin Android アプリケーション内で SQLite.Net を使用しています。次のようなエンティティがあります。
[DataEntity]
public class AuditEntity
{
[PrimaryKey]
public Guid EntryId { get; set; }
public string Action { get; set; }
public string GeoLocation { get; set; }
public DateTime Time { get; set; }
}
次のような一般的な挿入メソッドがあります。
public int Insert<T>(T obj) where T : class
{
var result = 0;
try
{
result = Connection.Insert(obj);
}
catch (SQLiteException ex)
{
Mvx.Trace(MvxTraceLevel.Error, $"Exception while inserting into database:\r\n{ex.ToLongString()}");
}
return result;
}
このメソッドに次のオブジェクトを渡します。
var auditEntry = new AuditEntity
{
EntryId = Guid.NewGuid(),
Action = uri.ToString(),
GeoLocation = geoLocation,
Time = DateTime.Now
};
時折、「制約」というメッセージとともに SQLite 例外がスローされますが、これは主キーの違反であると理解しています。
(自動的に作成された) テーブルの EntryId 列の型は varchar(36) です。
挿入が時々例外をスローするのはなぜですか?
パラメータ化されたコマンドを介して挿入を行う非ジェネリック メソッドを使用すると、例外は表示されませんが、ジェネリック メソッドを使用したいと思います。