(Silverlight) Switching between LINQtoSQL and Entity Framework

For my current project, I decided to switch from LINQToSQL (Linq) to ASP.NET Entity Framework (Entities) – mainly because going forward, it seems to be what Microsoft are supporting. They’re similar, and they’re different. The similarities, if you’re used to Linq, might be what topples you.

Fubar Singularization / Pluralization

First of all, Linq names stuff exactly like I’d expect. It tries to Singularize single members and Pluralize member sets (if you need to Singularize or Pluralize, check this out). Types are called in the singular, wherever possible. Or perhaps that only works when your tables are named in the Singular to begin with.

Entities seems to go about it totally hodge-podge.

My tables are, by chance, named in plural and in singular, so I’m stuck with the worst of two worlds. The plural naming of tables, I blame on Microsoft, I always name my tables in the singular. My tables are named as follows;

  • aspnet_Users
  • Catalog
  • Image
  • UserVote
Linq

Linq properly singularizes aspnet_Users to aspnet_User and leaves the other types in the singular. It names the table accessor methods in the plural (to access all catalogs, I’d type contexct.Catalogs). Parent members are named in the singular- and Image belongs to a Catalog, so a Catalog has a property called Images and an Image has a property called Catalog. Makes perfect sense?

LinqNaming

Entities

Right off the bat, why oh why is the Visual Studio template called “ADO.NET Entity Data Model”? I have no idea what ADO.NET is… Do I need to know? The Linq template is called “LINQ to SQL Classes”. Does it use ADO.NET? Does it use something else? I have no idea – and so far, I haven’t needed to know.

EntitiesNaming

There’s really no doubt that the entities renderer is nicer than the Linq renderer, but not by much. In the Linq case, the child EntitySets aren’t visible in the renderer, nor are the parent entities. But take my word for it, they’re correctly named – even though the Entities versions aren’t.

aspnet_Users are in plural – why is that? Linq manages to make it singluar… The owning field of the Catalog is called “aspnet_Users” – thought it’s a single user. Again, Linq comes out on top with a singular. The list of images in a catalog are called… Image! That’s a damn singular! In Linq, the type is called Image but the list (in the Catalog) is called Images. Entities, what’s wrong with you?

the TO My types wind up in plural, my owning members in singular, my child object sets also in the singular – what gives?

Foreign Entity / Foreign Key

Linq

In Linq, I’m used to entities containing both the parent entity and the parent key – you can’t set both, but that’s fine;

An image belongs to a catalog, so an image has an Catalog Entity (of the Catalog type) and an CatalogId foregin key. If I set the Catalog property, the Catalog is set to null behind the scenes.

I can set the CatalogId property, if I don’t really care about the details of the catalog – I just want to hook up that foreign key. If I try to set the Image.CatalogId proeperty after I’ve set the Catalog property, an exception is raised – which is only fair. Make up your mind, access the foreign key or the entity reference?

Entities

In Entities, the Image entity doesn’t have an CatalogId foreign key, it only has the Catalog entity! I’m not at all sure how to work with that! In Linq, I’d write a query to locate all images in a particular catalog as follows;

context.Images.Where(image=>image.CatalogId==catalogId);

But in Entities, I’m forced to do this;

context.Images.Where(image=image.Catalog.CatalogId==catalogId)

or perhaps

context
    .Catalogs
    .Where(catalog=>catalog.CatalogId==catalogId)
    .First()
    .Images();

 

The first alternative isn’t so bad, if it’s efficiently converted to SQL, I’m guess it might even be better… but when you wish to add an Image to an Catalog, and you only know the CatalogId, what to do?

In Linq, you’d simple do something like this;

Image image = new Image{ CatalogId=5, Name = "Böööby"};
context.Images.InsertOnSubmit(image);
context.SubmitChanges();

 

But with Entities, the Image object doesn’t contain a CatalogId propety! Do you load the Catalog from the context before you start (cumbersome, especially with Silverlight)? Do you create a fake catalog that just contains the correct CatalogId? How do you signal that it’s fake so that it doesn’t get created again?

FINISH UP

At this point using Entities, I decided to revert to using Linq instead, because trying to make really trivial stuff work turned out to be too much hassle…

About mfagerlund
Writes code in my sleep - and sometimes it even compiles!

One Response to (Silverlight) Switching between LINQtoSQL and Entity Framework

  1. May I suggest that you look at the new version of the Entity Framework (called EF4) which is part of .net 4 and vs2010. You can get the RC version of both these products now.

    This release should address each of the issues you brought up above. I think you may find it worth another look.

    – Danny

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: