替换 sqlite-extensions:InsertWithChildren(变量太多)

本文关键字:变量 太多 InsertWithChildren sqlite-extensions 替换 | 更新日期: 2023-09-27 18:31:29

我这里有一个项目,从不同的来源读取了大量数据。在特殊逻辑中,使用这些数据构建数据/对象模式。因此,我检索了一个完整的SQLite对象模型。

数据以前使用简单的方法写入SQLite数据库:

  _connection.InsertWithChildren(model, true);

但是,由于数据源变得更大,这不再可能,因为 Insert 方法将引发"变量过多"异常。 ;(

现在,我正在寻找这种方法的替代品。这里的困难在于,在我的模型中,我几乎总是在两个方向上都有外键。父母有孩子,孩子知道父母。

性能不是问题。我不在乎函数是否需要 10 秒或 5 分钟。但是有没有人知道如何处理插入,同时正确填写所有外键?

如果我使用简单的

foreach(var entity in _entityList)
  _connection.Insert(entity);

外键 (ID) 都是 Guid.Empty;

最好的问候和欢呼,

克里斯

替换 sqlite-extensions:InsertWithChildren(变量太多)

在问题 #64 修复之前,您可以在列表中使用ReadOnly属性。

例如:

public class Foo
{
    [PrimaryKey]
    public Guid Id { get; set; }
    [OneToMany(ReadOnly = true)]
    public List<Bar> Bars { get; set; }
}
public class Bar
{
    [PrimaryKey]
    public Guid Id { get; set; }
    [ForeignKey(typeof(Foo))]
    public Guid ParentId { get; set; }
    [ManyToOne]
    public Foo ParentFoo { get; set; }
}

无论执行什么操作,都不会再遇到变量限制问题。


您现在可以安全地插入元素:

// Insert parent 'foo' element
// This won't insert the children or update their foreign keys
conn.InsertWithChildren(foo); 
// Insert all children
// This will also update ParentId foreign key if ParentFoo property is set
conn.InsertAllWithChildren(bars)

或者使用普通 SQLite.Net 方法自己分配外键:

conn.Insert(foo);
foreach (var bar in bars) {
    bar.ParentId = foo.Id;
    conn.Insert(bar);
}