CRUD operations with Optimistic Locking using Silverlight 2 Beta1, WCF and LINQ to SQL (inserts, updates and deletes)

Update 17.4.2008: I have found a bug on the LinqToSqlClientHelper.LinqHelper class. I have attached the new corrected project.

In this 30 minutes video blog I will show you one approach on how to do insert, update and delete operations with optimistic locking in a 3 tier environment. As you may know LINQ to SQL do not offer client side change tracking out of the box so you need to implement your own. I will also show a master detail example.

To keep things simple I tried to mimic the behavior of DataSet treating every entity (e.g. Customer, Order, etc..) as separated entities and not sub collections. This makes the pattern and the LinqHelper class much simpler and also don’t requires to do Eager loading on the service implementation.

Attached the full sample including the two LinqHelper projects. Remember to change the connection to your DB and to download the Northwind sample DB.

You should watch first this video blog that teach you the basics:

In this part 2 video blog I will explain you what I’m doing in the LinqHelper classes:


          it is not production code, it is a sample to show one approach to change tracking in multi tier architecture with LINQ to SQL

          there are for sure better solutions and feel free to submit your ideas and improvements.

          works only with tables with Primary Key as single column

          works with timestamps or original values

          it is not fully tested (or better is not tested at allJ) with different tables/data types etc…

Last but not least many thanks to Ken Casada for the long brainstorming sessions in finding a simple but functional approach.

Have fun

Ronnie Saurenmann

double click the player to play in fullscreen


Comments (20)

  1. BenHayat says:

    Ron; Very, very nice!

    I think one thing that is super important in these cases, are having the client interface and how it syncs the "tracking" with middle tier and finally how the middle tier syncs the tracking with the database.

    It would be great in your next video if you could get more into the details for the tracking, which I’d consider an important part of the 3-tier development.

    Your video cleared up this important part very nicely and be assure that is very much appreciated!

    Thank you!


  2. Post: Approved at: Apr-6-2008 Display/Edit data – SL2.0B + WCF + LINQ to SQL Here are a couple videos

  3. Rocky Moore says:

    First, great post!

    There are a few of us that have been battling the issue of disconnected data with LINQ to SQL.  We have looked more at adding the functionality of change tracking of an object from within the object itself by mannually adding an original state value along with a clone() method to the object.  This works fine for the individual objects but does not handle the deleted objects as your solution does.

    Sure would be nice if LINQ to SQL supported built in original state option (as a property option to generate or not) along with a Clone() method that does a clone of the class without having to use reflection.  Then perhaps a new ChangeTrackingList<> collection derived from List<> that would track deleted items and be aware of and use the Clone() and original state features added to the entity objects.

    I guess the real dreams is a LINQ to SQL Disconnected DataSet 🙂

    Thanks again for the video, very helpful!

    The method you used works great allows for only modified data to be shipped back with is another plus.

  4. Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

  5. Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

  6. Here is the latest in my link-listing series .&#160; Also check out my ASP.NET Tips, Tricks and Tutorials

  7. In this 15 minutes video blog I will explain the magic (or not so magic J ) that I’m doing in the LinqHelper

  8. Вот здесь можно посмотреть предыдущий пост из серии списка постов. Также некоторые ссылки на мои популярные

  9. 【原文地址】 April 11th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, Silverlight 【原文发表日期】 Friday

  10. 11 april Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, VISUAL STUDIO, Silverlight ASP.NET Meer ASP.NET Beveiliging

  11. Voici l&#8217;article le plus r&#233;cent dans ma s&#233;rie Listes de liens .&#160; Visitez aussi ma

  12. ASP.NET: آموزش های امنیت بیشتر در ASP.NET : سه آموزش آخر و عالی اسکات میشل در مورد امنیت ASP.NET سه آموزش

  13. ASP.NET: آموزش های امنیت بیشتر در ASP.NET : سه آموزش آخر و عالی اسکات میشل در مورد امنیت ASP.NET سه آموزش

  14. As a user experience designer, the visual design of content I produce is an important consideration.

  15. You might have noticed that posts have dropped off over the last month or so.&#160; The reason being

  16. Anonymous says:

    Over at the Swiss MSDN Blog, Ronnie Saurenmann has posted some really great tutorials for using Silverlight with a SQL database over a webservice and also includes how to do things like CRUD operations and how to implement your own client side change

  17. (以下内容全部整理自博客堂Scottgu博客中文版)Silverlight技巧,诀窍,教程和链接 【原文地址】SilverlightTips,Tricks,…

  18. mabra says:


    Please, what do I need, to load this project sample into VS [I use 2008]. I does not load the LinqToSqlCLientHelper and the SilverlightApplication4 projects??

    Thanks a lot,


  19. shree says:

    Can this work in VS 2008, framework version 3.5? Its not opening the silverlight project in VS 2008, framework version 3.5.

  20. امیر کمسیونچی says:

    دوستان سلام  طریقه درست کردن کانکشن برای vpn  را به ادر س من در  بفرستید از اینکه زحمت میکشید ممنونم