我决定在我的 .NET 应用程序中使用 SQLite3,并且我在我的实体中定义了一些 System.Uri
属性。
我正在尝试使用 SQLite-net ,做:
var myEntities =
Enumerable
.Range(1, 100)
.Select(id =>
new MyEntity()
{
Id = id,
StringProp = $"{nameof(MyEntity)} #{id}",
DateTimeProp = DateTime.Now.AddDays(id),
UriProp = new Uri("...")
});
using (var conn = new SQLiteConnection(dbFilePath))
{
conn.CreateTable<MyEntity>();
conn.InsertAll(myEntities);
}
但我得到的只是:"System.NotSupportedException: 'Don't know about System.Uri'"
。
有没有办法继续使用 System.Uri
并将其保存为 string
?
非常感谢
最佳答案
这取决于你需要从 URI 中得到什么,假设你想要绝对 uri:
string uristring = uri_variable.AbsoluteUri;
您还可以检索用于构造 uri 的原始字符串:
string uristring = uri_variable.OriginalString;
这样你仍然使用 URI,但你将它作为字符串保存在 SQLite 中。
编辑:
该库抛出异常(Sqlite.cs 的第 2080 行),因为未考虑类型 System.Uri。 我认为它可以添加到列表中,然后转换为 varchar(100) 作为示例。当然这还没有经过测试,我相信将它保存为字符串应该是可行的方法。
关于c# - 在 Sqlite3 中存储 System.Uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40933915/