替换sqlite扩展:InsertWithChildren(太多变量)

时间:2016-01-21 17:45:56

标签: c# sqlite sqlite-net-extensions

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

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

  _connection.InsertWithChildren(model, true);

但是,由于数据源变得更大,这是不可能的,因为Insert方法会抛出“太多变量”异常。 ;(

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

性能不是问题。我不在乎函数是否需要10Seconds或5Minutes。但有没有人知道如何处理插入,而所有外键都正确填充?

如果我使用简单的

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

外键(ID)都是Guid.Empty;

最好的问候和欢呼,

克里斯

1 个答案:

答案 0 :(得分:0)

在修复issue #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);
}