Home > English > Switching from Linq2Sql to Entity Framework

Switching from Linq2Sql to Entity Framework

There are just enough differences between Linq2Sql and Entity Framework that it can be very irritating. I have been migrating some source code from Silverlight 4 RC to the RTM and I decided that since Linq2Sql was no longer being considered a first class citizen, I would go ahead and migrate the data access layer as well. Easy right? Well, not exactly a walk in the park. I have been using WCF RIA Services as my transport layer for my Silverlight applications and it has worked out really well.

However, there are a few things that I liked better in Linq2Sql than in Entity Framework:

1. Loading associations
I think that the Linq2Sql team nailed it when they gave you the DataLoadOptions class with the LoadWith method.
You can see the difference yourself by looking at the following code snippet:

[Query()]
public IQueryable<Customer> ReturnCustomersIncludingProducts()
{
    DataLoadOptions loadOpts = new DataLoadOptions();
    loadOpts.LoadWith<Customer>((o) => o.Products);
    this.DataContext.LoadOptions = loadOpts;
    return this.DataContext.Customers;
}

Now look at the same example in Entity Framework:

[Query()]
public IQueryable<cg_f_Form> ReturnCustomersIncludingProducts()
{
    return this.ObjectContext.Customers
        .Include("Products")
        .OrderBy(c => c.CustomerName);
}

What I don’t like about the Entity Framework implementation is that it uses magic strings and there is no compile time checking. At least with the Linq2Sql version you could get an error if the underlying model changed.

2. DomainDataSource
Another issue that I have with the Entity Framework is the added work that you need to do in order for paging to work correctly. In Linq2Sql you just add your paging criteria and then went on your happy way. The Xaml side code is provided below:

<riaCtls:DomainDataSource 
    x:Name="ddsMasterForms" QueryName="ReturnCustomersIncludingProductsQuery" AutoLoad="False" LoadSize="50" PageSize="25"
    DomainContext="{Binding DomainContext, Mode=TwoWay}" />

In Entity Framework if you do nothing else and let your application run, you will get the following exception:

“Load operation failed for query ‘ReturnCustomersIncludingProductsQuery’. The method ‘Skip’ is only supported for sorted input in LINQ to Entities. The method ‘OrderBy’ must be called before the method ‘Skip’.”

If you look back at both version of the server Queries, you notice that you will have to add an OrderBy statement. It turns out that this is required because the DomainDataSource’s Load operation tries use the ‘Skip’ and this method requires that the underlying data source be sorted. This was never a requirement in Linq2Sql.

So there you have it, if you are transitioning from Linq2Sql to Entity Framework, you will find a few interesting issues. Hopes this helps….

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: