LINQ to SQL v LINQ to Entities

Whatare the differences between LINQ to SQL v LINQ to Entities, well:

LINQ to SQL uses tables as entities

LINQ to Entities allows me to write C# LINQ queries that run against a conceptual data model.

The following table provides a summary of the features within each LINQ object

So this means that LINQ to SQL uses a database model to create its entities, where as the Entity Framework work on a layer of abstraction above the data, this is a most important feature.  As most database changes are taken care of by the schema and mapping without requiring a change to the obect model – making it so you do not have to refactor and rebuild your objects.

The second difference is the Entity Framework has the ability to allow the entity inheritance and entity composition.  This means that you can create an entity that is composed of colums originating in multiple tables without any complex join logic.

In summary:

  • If you want the added security of insulation and loose coupling from the underlying database schema to make your object model more resilient to change, use the Entity Framework
  • If you find that you need the features of entity inheritance and entity composition, use the Entity Framework
  • If you already have a large LINQ to SQL codebase that is running just fine without entities, you probably don’t need to spend the time to refactor out LINQ to SQL to replace it with LINQ to Entities.
  • If you want to run LINQ queries against an object model, but your object model is a 1:1 mirror of the tables in your database, you probably don’t need the Entity Framework.

LINQ Entity Framework

Microsoft has released it’s Entity Framework to the work, so does this mean the death of LINQ to SQL?  Check out my blog on this, Death of LINQ to SQL

So why is LINQ so good, well the problems with data has always been around the different and many way you need to know to interface and programme to.  For example if you conntect to a Microsoft SQL Server database, you need to know SQL, to read and write to XML files you need to learn XPath or XQuery, to query Active Directory you need to know LDAP (Lightweigth Directory Access Protocol).  To know all of these are more is quite a task.

SO along comes LINQ, which is a intergrated query language, which provides the same query language and grammer features, no matter where the data is held.

Microsoft started off by allowing developers to use LINQ to XML, LINQ to L, and LINQ to Objects.  These are all good starting blocks, but I have always been aware that Microsofts intention has been to pull all of these together and under one method, and that is the Entity Framework.  Which is built on top of LINQ and ADO.NET 3.0.

In order to use the Entity Framework you will nedd Visual Studio 2008, along with Service Pack 1 and .NET 3.5

Defining an Entiry Data Model

Once you have created your project, say an Console Application, you can get down to generating an Entity Data Model

 

  1. Make sure you have Visual Studio 2008 SP1, otherwisethe Entity Framework will not be available.
  2. Add a New Item to your project, and select ADO.NET Entity Data Model.
  3. Change the name to read NorthwindModel.edmx
  4. Select to Generate From Database
  5. Choose you data connection you wish to use, select the Northwind database
  6. Save the connection, you should find your app.config should now have the NorthwindEntities
  7. Select the database objects you wish to choose, in ower example select Customer, Orders, Order Details and Products.
  8. Change the Model Namespace to NorthwindModel and click finish.
You should end up with an .EDMX file looking something like this:

Okay, now it is time to query the data

Querying Products

What is nice about LINQ is it looks and feels very much like SQL.  With one exception, you have to write the syntax back to front.  Why I hear you ask, I don’t know the official answer to this, but my thoughts are that it is due to intellisense, as by writing you syntax backwards the intellisense can find the information you need.

Okay to query the Products table, you will first need to create an instance of the NorthwindEntities Class, where did this come from?  Well it was created when you created the .edmx file.

 

NorthwindEntities entities = new NorthwindEntities();

Dim db As New NorthwindEntities()

 

Next we have to create the LINQ statement to read the Products table / entity

 

var products = from p in entities.Products

                           where p.ProductID == 7
                           select p;

Dim products = From p In entities.Products _
                       Where p.ProductID = 7 _
                       Select p

Okay now you have the products you can loop through the list by using a For Each method
foreach (var p in products)
{
      Console.WriteLine(p.ProductName);
}
For Each p In products
      Console.WriteLine(p.ProductName)
Next
How simple could it be?
Check out my other blog entries in using LINQ to find out more information on what you can do with LINQ

To find out more about LINQ and the Entiry Framework check out The ADO.NET Entity Framework OverviewThe ADO.NET Entity Framework Overview or Introduction to ASP.NET Dynamic Data

Create SubVersion on an Internet machine via port 80

 

It’s nice to use SubVersion locally, after seeing that CodePlex now supporting SVN over HTTP, I thought I’d give it ago.  

Here all all the steps for allowing you to use SVN via port 80 and SVN:

The subversion server, download the source, not third party application, installer at 

http://subversion.tigris.org/project_packages.html

does not include Apache, it includes the subversion modules that work with apache but not apache itself.

 

After installing the package you will need to set svnserve up to be a service, there are instructions for this at:

http://svn.collab.net/repos/svn/trunk/notes/windows-service.txt

Essentially you need to do:

md c:\subversion-repositories

sc create Subversion binpath= “c:\program files\subversion\bin\svnserve.exe –service -r c:\subversion-repositories” displayname= “Subversion Server” depend= Tcpip

 

Then

net start Subversion

 

This sets up the subversion service and starts it, you will then need to create your repository

“c:\program files\subversion\bin\svnadmin” create c:\subversion-repositories\repos

 

You now have a svn server running with a repository called my-repos available at svn://<server-ip>/repos

 

At this point the repository will be set to anonymous read access and auth write access but will not have any authentication setup, to set this up go into the c:\subversion-repositories\repos\conf folder and edit authz and passwd files to setup the permissions, they are comments and fairly easy to understand.

I’ve attached two working examples for authentication, to help the configuration.

svnserve.conf (413.00 bytes)

passwd (51.00 bytes)

Simple LINQ 2 SQL example

I’ve been using LINQ to SQL for some time, and each time I come back to the same example, so it’s about time I shared the example:

 

      //  Create a DataContext.
      Northwind db = new Northwind();

 

      //  Retrieve customer LAZYK.
      Customer cust = (from c in db.Customers
                       where c.CustomerID == “LAZYK”
                       select c).Single<Customer>();

 

      //  Update the contact name.
      cust.ContactName = “John Smith”;

 

      try
      {
        //  Save the changes.
        db.SubmitChanges();
      }

      //  Detect concurrency conflicts.
      catch (ChangeConflictException)
      {
        //  Resolve conflicts.
        db.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
      }

 

January 2009 CTP of the Windows Azure Tools and SDK released

Microsoft have release the January CTP edition of the Windows Azure Tools and SDK Kit

Windows Azure Software Development Kit (January 2009 CTP)

Windows Azure Tools for Microsoft Visual Studio January 2009 CTP

These newly released SDK and Tools include:

  • Bug and performance fixes
  • Improved integration with Visual Studio
  • Performance improvements with execution and debugging scenarios
  • Improvements to Storage Client and ASP.Net provider samples
  • Added support to debug Silverlight in a web role