Be mindful of your DataContext's "context"

In a previous post here [http://geekswithblogs.net/michelotti/archive/2007/12/25/117984.aspx], I discussed implementation of Attaching Linq entities to a DataContext.  In that post, I showed an implementation of utilizing a Detach() method that I originally based on this post here [http://weblogs.asp.net/omarzabir/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx] .  The implementation boils down to the need to reset EntityRef<> references back to their default...


Attach an entity that is not new, perhaps having been loaded from another DataContext.

This exception using the Linq Attach() method is somewhat perplexing at first: System.NotSupportedException: An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported. This blog post here [http://weblogs.asp.net/omarzabir/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx] sort of pointed me in the right direction. But I found the WHY confusing and I found the example confusi...


Linq Table Attach() based on timestamp or row version

In a previous post here [http://geekswithblogs.net/michelotti/archive/2007/12/17/117791.aspx], I showed an example of using the Attach() method in conjunction with a Timestamp column in your database table.  In listing options that are supported, Microsoft's documentation states: "Optimistic concurrency based on timestamps or RowVersion numbers."  So what are some alternatives to using a Timestamp column in your SQL Server database?  It turns out, this is pretty simple.  Two other alternatives a...


Linq Table Attach()

The ability to use live Linq queries right in your UI makes for great demo's, but it doesn't bear a striking resemblance to a real-world, professional application which uses tiers.  In traditional n-tier applications, you want to have a strong "separation of concerns" and encapsulate your business layer, your data layer, and your UI layer distinctly.  One of the nice things about Linq is that the flexibility is huge.  If you want to do live queries in your UI, fine.  If you want to encapsulate L...


C# 3.0 Extension Methods - Real World Example

C# 3.0 Lambda Expressions play an integral part of making the LINQ framework work.  Outside of LINQ it is recommended that they be used sparingly because they are less "discoverable."  However, there are a couple of nice scenarios where extension methods have good potential to make your code more elegant as a stand-alone language enhancement. Take an example where you have a nullable value in the database and you represent this as a Nullable on your C# object (e.g., EmploymentEndDate).  When y...