SpriteHand
Module Border
  LINQ to SQL CRUD
Module Border
Location: BlogsAndy's Blog    
Posted by: host 3/28/2008 12:42 PM

I'm seeing a lot of buzz around LINQ to SQL lately and how it can help to quickly create a Silverlight 2 data app.

I'll be giving a talk around this at the upcoming Code Camp 9 in Waltham, MA, and I wanted to share how I am doing basic CRUD operations in LINQ to SQL, as I've seen this question come up a few times.

You may remember my Silverlight 1.1 Alpha demo app, "Andy's Fridge." I'll be using a similar Beer Database which has a very simple schema containing Beers and Brewers like so:

It's important to note that the RowVersionId columns in the diagram are Timestamp columns. This will allow LINQ to SQL to generate more efficient Update statements, based on the unique Row Id (timestamp) instead of Where clauses with lots of logical "AND's" for each original value.

So, given that schema is like that above, our CRUD operations for the Beer entity would exist in our web service like so:

public class BeerService : IBeerService

{

 

    public List<Beer> GetAllBeers()

    {

        DataClassesDataContext db = new DataClassesDataContext();

        var beers = from b in db.Beers

                    orderby b.BeerName

                    select b;

 

        return beers.ToList<Beer>();

    }

 

    public void SaveBeer(Beer beer)

    {

        DataClassesDataContext db = new DataClassesDataContext();

 

        if (beer.BeerId > 0)

        {

            // this is an update

            db.Beers.Attach(beer, true);

        }

        else

        {

            // this is an insert

            db.Beers.InsertOnSubmit(beer);

        }

        db.SubmitChanges();

    }

 

    public void DeleteBeer(Beer beer)

    {

        DataClassesDataContext db = new DataClassesDataContext();

        db.Beers.Attach(beer, false);

        db.Beers.DeleteOnSubmit(beer);

        db.SubmitChanges();

    }

}

 

Note that I piggybacked the SaveBeer method to do either an Insert operation or Update operation, based on whether or not a primary key is available in the passed entity.

I guess until ADO.NET Data Services (Astoria) and the Entity Framework are officially available, we will have to settle with LINQ to SQL for Data Access! But I don't think we will be waiting long :)

What can we look forward to with Astoria and the EF?

  • no need to write tedious web service methods for CRUD operations like shown above
  • serialization of Parent and Child records (LINQ to SQL cannot pass related child records across the wire with parent rows)
  • Access to other Database Providers other than SQL Server.
  • The ability to flexibly map Conceptual model elements such as class properties to Physical columns in the database (LINQ to SQL provides only a 1-to-1 mapping)
Permalink |  Trackback

Comments (2)   Add Comment
Re: LINQ to SQL CRUD    By Anonymous on 3/30/2008 9:07 PM
Hi,
How to write the comment in your blog? Where to specify the name or url or email or etc

Re: LINQ to SQL CRUD    By Anonymous on 7/6/2009 2:03 PM
This got me started on the whole LINQ to SQL bit and cleared up a few cobwebs for me on it. However, do you happen to have a sample that shows how to do this without a RowVersionId or Timestamp field?


Title:
Comment:
Add Comment   Cancel 
Module Border Module Border
Module Border
  Subscribe
Module Border
RSS   Twitter
Module Border Module Border
Module Border
  Diversions
Module Border

TALKING RAGDOLL
This Windows Phone 7 App was created using Silverlight, the  Physics Helper Library,  and the Farseer Physics Engine. It gets interesting when you import your friends photos and have your way with them!

MORE INFO



DROPPYPOP
This Windows Phone 7 game was created using Silverlight, the  Physics Helper Library,  and the Farseer Physics Engine.
DEMO

MORE INFO



BOSS LAUNCH
This physics game won first place in the Server Quest Contest. Created using Silverlight 2, the Physics Helper Library,  and the Farseer Physics Engine.
PLAY IT

MORE INFO



DESTROY ALL INVADERS
A scrolling shooter game where the objective is to destroy the invading UFO's flying over a neighborhood of your choosing. Imagery provided by Microsoft Virtual Earth. Created using Silverlight 2.
PLAY IT

INFO AND CODE



PHYSICS HELPER DEMOS
These demos were created for the Physics Helper Library, which makes it easy to create physics games and simulations using Expression Blend, Silverlight, and the Farseer Physics Engine.
PLAY IT

INFO AND CODE



HOOK SHOT
This little basketball game took first place in the TeamZoneSports Silverlight Contest. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SORT THE FOOBARS
A game where you need to sort the good foobars from the bad ones. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



POLYGON PHYSICS DEMO
A demo showing polygon physics where the user draws physics objects with the mouse. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SILVERLIGHT ROCKS!
Destroy the asteroids before they destroy your ship! Created using Silverlight 2.
PLAY IT

INFO AND CODE



FISH GAME
A simple game of harpoon-the-fish. Written using the AJAX Sprite Toolkit.
PLAY IT

INFO AND CODE

Module Border Module Border
Module Border
  Search_Blog
Module Border
Module Border Module Border
Module Border
  Blog_Archive
Module Border
Module Border Module Border
Copyright (c) 2013 andy.beaulieu.com - Login