Saturday, June 7, 2008

Data Access with LINQ to SQL - Update

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:

  1. 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.
  2. 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.

8 comments:

Anonymous said...

When I attempt to download your code using TortoiseSVN, it prompts me for a username & password. is there are way to download your code anonymously? If not, how do I get a username and password?

Sorry for the newbie question, I haven't downloaded any source code from GoogleCode before.

thx
Scott

Anonymous said...

Just tried it again, no issues - sry for the early post.
Scott

Anonymous said...

This is great stuff Brian. I have searched for days on how to do something like this. Thanks!

Dave

scharby said...

Brian, converted your application to VB.NET. The 'SerializerUtil' C# class has to be converted to a VB module. My first attempt, with all code within same namespace, worked fine. Second attempt, I broke out app.Business, app.Web projects/namespaces. The problem is 'SerializerUtil'. It is used in app.Web but I must be declared in app.Business. As a module, is has no scope visability in app.Web. Any suggestions (other than don't use VB)?

Brian said...

Just move the SerializerUtil to your business layer and you should be good to go. Since your Web project points to your Business Layer, it should still be able to call it just fine. Remember, Namespaces != Assemblies/Projects.

Anonymous said...

Hey, i am trying to download the code using tortoise SVN and it is asking for Username and Password. I have tried few times but no luck.

Could you please let me know if there is any username and password ot download a code from Google Code.

I have never done it before.

Jason said...

Hi scharby,

Could you send the VB.NET version of the code? I'm wanting to do the same thing.

Thanks,
Jason

Aaron said...

I am using LINQ across n-tier web application using WCF. I have been able to get all of the back-end tiers working fine. My problem is with the DetailsView. I have two business methods, GetContacts() and GetContact(long contactID). I use GetContacts() to load my GridView and that works fine since it returns a List{Contact} object. However, when one of the contacts is selected and I call GetContact() which returns a LINQ Contact object and I try to bind it to my DetailsView, I get an unbindable error. The only posts I found on the error say to put into a generic List so it's IEnumerable, but it still won't display the information from the object. What could be wrong here and how should I bind the LINQ object coming from my business method to the DetailsView?