LINQ to SQL Tips 7: Minimal update when you don’t want optimistic concurrency check

The common guidance for updates is to keep the original values in view state so that you can recreate the object in its original state, Attach() it and then set the values that the user has modified. This works well for the most part. But at times, a user is allowed to to set only…

2

LINQ to SQL Tips 6: How to refresh given DataContext won’t stomp on your objects

The DataContext is a rather careful beast. Once an object is retrieved, the DataContext will not stomp on it if a query returns the same object again. This is intentional. Imagine the chaos if you modified some of the retrieved objects or even read the values and made some decision based on that and then…

5

LINQ to SQL Tips 5: Using stored procs that return multiple results

Often I get the following questions about stored procs in LINQ to SQL: I have sproc that returns multiple results. How can I use it in LINQ to SQL? Can I use a sproc that returns results of different shapes; e.g. Customers and Orders? I drag-dropped a sproc returning multiple results on the designer surface….

7

LINQ to SQL Tips 4: Use DeleteOnNull if you want to delete object with null FK

I often get a question along the following lines: If I remove one of the OderDetails from Order.OrderDetails collection, I see that the reference OrderDetail.Order is set to null but this just orphans the OrderDetail; it is not deleted. How can I change that? Here is how: This is not exposed in the designer so…

5

From LINQ team to App Framework on Silverlight team

A while ago (October last year), I moved from C# team to the “UI Frameworks” team inside the .NET Developer Platform team. C# team is where LINQ was incubated and large portions of LINQ were productized: LINQ to Objects, LINQ to SQL and C# 3.0 to be precise. So it was with mixed emotions that…


Lifetime of a LINQ to SQL DataContext

One of the frequently asked questions is about the lifetime of a DataContext. Should it be a long-lived, application-scoped object or should it be a short-lived, request-scoped object? Let’s get to the answer by considering the key parameters: DataContext is ideally suited for a “unit of work” approach: Retrieve a bunch of objects through one…

11

LINQ to SQL Tips 2: how to use common base class for all entities

Here is another question I get quite often: I have some common fields (maybe id and timestamp) and some common behavior across all my entities. How do I handle that in LINQ to SQL? After all, the only inheritance mapping supported is Table Per Hierarchy (TPH in ORM jargon). Here is a two part answer:…

6

LINQ to SQL Tips 1: how to map an enum

I was out on vacation (Zion, Bryce Canyon and Grand Canyon National Parks) so my blog went dark for a while. Also, I noticed a few comments that were incorrectly flagged by the spam filter – I have now published them, albeit after a delay. Sorry about that delay. As we get closer to a…

8