c# - 在 Sqlite3 中存储 System.Uri

标签 c# .net sqlite sqlite-net

我决定在我的 .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/

相关文章:

json - SQLite 扩展二进制文件

C# 非托管 dll 导出(它是如何工作的)

c# - 如何确定将在矩形中绘制的文本的高度?

c# - 如何在 Asp.Net Web API 上实现 Https/SSL 连接?

c# - .NET 自定义 Xml 序列化

android - SQLiteLog :(1) no such table: tableName. db (sqlite with android)

java - 将数据插入数据库 - 为什么我使用 -1 而不是 0?

c# - 与 Windows 服务交谈

c# - SlimDX DirectInput 初始化

.net - ConfigureAwait 有什么作用? ASP.NET Web API 2