代码之家  ›  专栏  ›  技术社区  ›  Zachary Scott

实体框架4:保存层次模型的更有效方法

  •  1
  • Zachary Scott  · 技术社区  · 14 年前

    Product[].properties
             .SET1[].properties
             .SET2[].properties
                    .SETA[].properties
                    .SETA[].SETb[].properties
    

    当我去保存这个时,我有以下流程:

    Grab DB Product that matches current
    Update Properties
    Grab and loop through set 1 from DB
        If not found in "to save" set, delete item from set 1
        If found, update it
    Grab and loop through set 2 from DB
        If not found in "to save" set, delete item from set 2
        If found, update it
        While we are in Set 2, grab all Set A from DB
            If not found in "to save" set, delete it from set A
            If found, update it
            While we are in Set A, grab all Set b in DB
                If not found in "to save" set, delete it from set b
                If found, update it
    
    Now go back through and insert any in local set not in DB
    

    这一定是我见过的最糟糕的更新算法。有谁有更好的代码或链接,可以更简单一点?当前正在使用C#

    1 回复  |  直到 14 年前
        1
  •  1
  •   Pieter van Ginkel    14 年前

    您需要做的是找到一种方法,在一个查询中获取所有记录。方法是使用表示层次结构的类路径字段。例如:

      • 代码为PROD1-PROP1的属性1

        • 集合1的属性1,代码为PROD1-SET1-PROP1

        • 集合1的属性2,代码为PROD1-SET1-PROP2

    等。

    现在,可以通过从路径“PROD1-”开始获取所有属性来获取所有属性。然后,在一次性检索完产品1的所有属性之后,就可以在内存中执行所有突变,并将所有更改存储在一个批中。