As with all new technologies, you continue to learn as you work with it. Some of my thoughts have changed from my previous post on using LINQ to SQL.
After attending TechEd 2008 in Orlando this past week and speaking recently on this topic at the Austin Code Camp, I have a (what I think) is a better approach to doing data access with LINQ to SQL in an ASP.NET web project. First, it is definitely my preference to separate all data context activity to the business and/or data tiers.
Some key things that I have learned along the way. You will want to use the Attach(entity, true), which essentially tells LINQ to "trust me, the entity has changed, update the whole thing." In order for this to work with an entity that has relational entities such as the Customer with the CustomerAddress, you must do one of the following things:
- Serialize and then deserialize the entity using the DataContractSerializer. This will be automatic if you are passing it over WCF, otherwise, you will need to write the code to essentially clone your entity.
- Use the DataLoadOptions to get the full entity graph when you pull down the original entity.
I've started storing my code up on Google Code. Here's a link to download the latest bits. If you aren't already an SVN user, just download TortoiseSVN-- trust me, it's easy.

Save This Page