Archive for August 18th, 2010
Entity Framework – It’s the little things…
by Andrew Tobin on Aug.18, 2010, under Uncategorized
So I have been working with Entity Framework for a short time on a sekrit projekt at the moment and even just with my beginner experience, and very limited need for special features, I keep running into issue points, so I thought I’d enumerate some here.
Now, for reference, I am using Entity Framework 4, CTP4 aka Magic Unicorn Edition.
1. Guid Primary Keys with Clustered Indexes.
You can probably find a lot of guidance online about why you don’t want Guids in a clustered index, but the basics are that with a clustered index, it tries to keep the index sorted, and Guids by their very nature are random.
So what you’re doing is, every time you insert or delete a row into your table, using an object that has an ID with a type Guid, it makes the SQL server work to find the right place to do the insert, puts the row in, and then has to re-sort the index to be correct.
The correct behaviour would be to keep it as Primary Key, so the Guid/uniqueidentifier is unique, but to have a non-clustered index – which means the key is still indexed, it can still find the key quickly and efficiently, but it doesn’t make the server work to keep it sorted in order.
The way it stands now, the Clustered Index will cause severe performance issues when as the amount of data in your database increases.
I have listed a Microsoft Connect article here to fix the issue.
The guidance I got from one of the team was to include some DDL scripting to drop and recreate the index, or make my own EF Provider, as a wrapper around their provider, to do the table creation properly – but I would like to see them fix this, as most of us would just accept what Microsoft are doing as correct.
2. The ability to Exclude properties from mapping to the Database.
Say for example, I have a property that I require in my object model for the view – say I want to collect the user’s email address, so I can create a Gravatar string (which is an encrypted hash of the email address) and then just save the string in my database, and not have to store their email anywhere.
Well, at the moment, whatever is in my object model will get mapped to the database (and if I allow it, Entity Framework will automatically create the database table with the email address in it.
There seems to be two ways around this at the moment:
1) Create the property with no setter, so it is read only.
2) Create the mappings by hand.
In the first instance, I cannot populate the address from the view, which makes it unusable, and in the second, I would have to manually map the other 40 properties, in code, and make sure I remember to update or map any future changes and additions.
The alternative would be for them to add another attribute, so I could do something like:
[Exclude]
public string EmailAddress { get; set; }
As far as things go, it’s a difficult learning process, as most guidance out there seems to be around the older way of doing Entity Framework, using model-first or at least the model designer.
I would love to see more information on dealing with POCO classes and the CTP4 out there, in an more centralised fashion, through the group’s blog or some better screencasts.
For example, finding information about how to change the schema prefix from “dbo” to your own, in EF seems impossible – I have a few ideas to try, but at the moment, it’s not the easiest framework to get into for a n00b.
I’ll possibly update this post as other issues come up, but if you have any suggestions on how to get around some of these problems, that I’ve overlooked – feel free to leave them, or links to helpful resources in the comments.