Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework POCO Change Tracking Strategies

I have an N-Tier application where POCOs are populated by Entity Framework on the server side and transferred to my client applications. The clients make changes to the POCOs or add new POCOs and then send them back to the server to be stored in the database.

If I am using pure POCOs, i.e., no proxies, not self-tracking entities, what are some of the common approaches people are taking to solve the Change Tracking issue? If your service receives a collection of POCOs, how does it know to do an Add, Update or Delete using Entity Framework?

like image 610
user235311 Avatar asked Sep 27 '13 21:09

user235311


1 Answers

Entity Framework doesn't have good built-in support for such disconnected scenarios. I am aware of three general options:

  • Use GraphDiff, an open source add-on library

    Advantages

    • No need to write change tracking code on client side
    • Common pattern to update disconnected object graphs in the database
    • Not much code to write on server side


    Disadvantages

    • Database must be queried and entities have to be loaded to detect if an object has to be added, updated or deleted
    • Dependency on a third party library in addition to the EF core libraries


  • Update object graphs manually on server side (Example)

    Advantages

    • No need to write change tracking code on client side
    • No dependency on a third party library in addition to the EF core libraries


    Disadvantages

    • Database must be queried and entities have to be loaded to detect if an object has to be added, updated or deleted
    • No common pattern, i.e. most update scenarios require individual code
    • A lot of code to write on server side


  • Add properties for entity states to your objects and track changes manually on client side by setting the states accordingly (I don't have an example for this approach; I believe, Julie Lerman is using and recommending it)

    Advantages

    • Database doesn't have to be queried to detect if an object has to be added, updated or deleted
    • No dependency on a third party library in addition to the EF core libraries
    • (Probably?) Common pattern on server side to translate tracked states into entity states of attached entities


    Disadvantages

    • Change tracking code to write on client side
    • No common pattern on client side, i.e. most change tracking scenarios (and client types/UI technologies) require individual code
like image 85
Slauma Avatar answered Oct 18 '22 03:10

Slauma