开发者

Entity Framework 4: More efficient way to save a hierarchical model

I have this Entity that has a lot of related records. When the web client saves, it sends up a JSON hierarchical model. The models looks like this:

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

When I go to save this, I have the following flow:

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"开发者_StackOverflow中文版 set, delete it from set b
            If found, update it

Now go back through and insert any in local set not in DB

This has to be the worse update algorithm I have seen yet. Anyone have better code or links that could be a little simpler? Currently using C#


What you need to do is find a way to get all records in a single query. The way you can do this is by using path like fields that represent the hierarchy. An example of this would be:

  • Product 1 with code PROD1

    • Property 1 with code PROD1-PROP1

    • Property 2 with code PROD1-PROP2

      • Property 1 of set 1 with code PROD1-SET1-PROP1

      • Property 2 of set 1 with code PROD1-SET1-PROP2

Etc.

Now, you can get all properties by getting all properties starting with the path "PROD1-". Then, after you've retrieved all properties of product 1 in one go, you can do all mutations in memory and store all changes in one batch.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜