0

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

数据之前使用一个简单的方法写入 SQLite 数据库:

  _connection.InsertWithChildren(model, true);

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

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

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

如果我使用一个简单的

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

外键(ID)都是 Guid.Empty;

最好的问候和欢呼,

克里斯

4

1 回答 1

0

问题 #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);
}
于 2016-01-27T17:54:04.233 回答