在C#中为SQLite动态生成一个类
我正在使用Xamarin.Forms.Net标准1.4库和SQLite nuget 我为数据库表/记录创建类,如下所示:在C#中为SQLite动态生成一个类,sqlite,Sqlite,我正在使用Xamarin.Forms.Net标准1.4库和SQLite nuget 我为数据库表/记录创建类,如下所示: namespace MyNamespace { [Table("my_table")] public class MyRecord { [PrimaryKey, AutoIncrement, Column("_id")] public int ID { get; set; } = -1; [Unique
namespace MyNamespace
{
[Table("my_table")]
public class MyRecord
{
[PrimaryKey, AutoIncrement, Column("_id")]
public int ID { get; set; } = -1;
[Unique, NotNull, Column("GUID")]
public Guid GUID { get; set; } = Guid.NewGuid();
public string Field1 { get; set; }
public string Field1 { get; set; }
}
}
我可以使用创建/插入表/记录
{
SQLiteConnection conn = new SQLiteConnection(db_file);
conn.CreateTable<MyRecord>();
var record = new MyRecord {
Field1 = "test",
Field2 = "test"
};
conn.Insert(record);
conn.Dispose();
}
{
SQLiteConnection conn=新的SQLiteConnection(db_文件);
conn.CreateTable();
var记录=新的MyRecord{
Field1=“测试”,
Field2=“测试”
};
连接插入(记录);
conn.Dispose();
}
我发现自己处于这样的情况:一个表在编译时没有已知的列,所以我无法创建一个类来描述它。有没有一种简单的方法可以动态生成类
假设在运行时,我需要创建一个包含以下列(全部文本)“NewField1”、“NewField2”的表。我意识到我可以生成SQL命令来处理数据。但是我失去了SQLite包装器的便利性
我想动态创建一个类,并根据需要添加属性,这样我仍然可以使用CreateTable和Insert()等。这可能吗?这样会有效率吗?或者我最好只是使用字典和创建SQL命令 不确定你想要达到什么目标。如果您在运行时生成一个表,那么您究竟为什么要在它周围生成一个包装器呢?你不会从这个类中得到任何编译时的好处(现在已经太晚了),我想我看到的好处是不必自己生成SQL查询。我想使用LINQ等