Entity Framework - Learning Materials recommended.
To date the best first up learning materials I have come across for EF have been the DNR TV web casts with Dan Simmons and Julia Lerman’s tutorials which I posted a PDF consolidation of recently. Dan Simmons gives some very good rudimentary ‘meet the Entity Framework’ demonstrations and I am beginning to quickly feel at home with EF. Having said that I will add my initial observations on the product so far (bear in mind I was a huge fan of LINQ To SQL).
- A better and more functional designer experience than LINQ to SQL
- Lacks the POCO ability that I crave (perhaps in V 2.x something)
- I like having the ability to map single entities to multiple tables, which LINQ To SQL could not do.
- Curiously, I have found that EF supports rapid development scenarios with an extremely similar approach to that of LINQ To SQL, therefore the argument to use LINQ TO SQL if your in a ‘hurry’ doesn’t really stack up for me anymore and I am questioning the validity of that proposal altogether.
Find the DNR TV episodes here:
To get more deeply into the product I just now ordered Julia’s book which I will have to wait till Jan 2009 for but there’s plenty to do whilst waiting, I am going to have a go at getting the specification pattern working with the Entity Framework as I did with LINQ to SQL previously and think about how to deal nicely with Fetching Strategies in EF.



I have my own issues with the Entity Framework atm. I’ve done one project and have gone back to LINQ 2 SQL for the time being. Maybe i’m miss using it but my issues are:
1) Lack of LazyLoading…
I find it fustrating that if i want to say, load an order and loop through all the order items i have to do…
order.OrderItemsReference.Load();
foreach (OrderItem item in order.OrderItems)
{
…
}
2) Entities are always pural, it was nice in LINQ 2 SQL where it made entities singular and puralized what you were selecting from, like you select a single Order from Orders…
Order order = context.Orders.SingleOrDefault(s => s.OrderID == orderID);
But in the EF you select a single Orders from Orders…
3) Having to load a related entity before comitting…
So i couldn’t do:
order.OrderStatusID = 1;
i couldn’t even do
order.OrderStatus.OrderStatusID = 1;
I had to do
order.OrderStatus = entity.Orders.First(s => s.OrderStatusID == 1);
I may be just doing something wrong so if anyone reads this and can correct my mistakes, please do.
[Reply]
Phillip
Lazy loading is a pain your right. LINQ to SQL got that right in my book but I have faith that EF will get there but it’s certainly not getting a pass mark for that yet.
As far as pluralization goes, you can edit the default naming behavior of both an entity and it’s ENTITY SET NAME property. This allows you to write:
Customer o = ctx.Customers.First();
[Reply]
@Phillip,
Here are a few responses to your comments above:
1) Lazy loading isn’t supported directly in the first release of EF (the one that shipped with .net 3.5sp1), but it will be an option with .net 4.0. Also, you can find a sample which adds this capability to .net 3.5sp1 in a series of blog posts here: http://blogs.msdn.com/jkowalski/archive/2008/05/12/transparent-lazy-loading-for-entity-framework-part-1.aspx
2) Automatic singularization / pluralization will also be added as an option in .net 4.0. In the meantime you might want to check out this tool from a company other than Microsoft which will add these capabilities to VS 2008 sp1: http://www.huagati.com/dbmltools/
3) While you can’t just directly set a foreign key property for these kinds of scenarios, you can create and set an EntityKey to accomplish the same thing without loading the related entity. In your sample above, you would do something like this:
order.OrderStatusReference.EntityKey = new EntityKey(”OrderStatus”, “OrderStatusID”, 1);
- Danny
[Reply]
Hey, thanks for the response’s.
I forgot to mention the main reason why i haven’t use the EF on any other projects atm.
Altho the ability to “Update” the entity model is useful, to update existing entities is a real pain.
If data type change, or relationships, columns are dropped, etc, these NEVER seem to get picked up. And if you delete an entity, this change isn’t reflected when you update, you end up having to delete the entire entity model and creating a new one.
This is really fustrating in the early stages of a project where the database is getting choped and changes 10 times a day, with LINQ2SQL its a simple case of deleting the entity and dragging it back on. It’s definitly less of a pain.
Is this a problem with the EF model or am i doing something wrong?
[Reply]
Hi all,
I’m not using EF right now for a live project but I see the lazy loading thing as an issue for me in future. My intended solution is use postsharp to weave aspects into my code at build time to intercept any calls to related entities and load them if necessary.
I wrote up what I’ve done so far (including sample code), I think the idea is sound but as always I’m open to critisism. http://pabloblamirez.blogspot.com/2008/10/entityframework-transparent-lazy-load.html
Regards,
Paul
[Reply]
Phillip,
Regarding dropped entities not showing up in the ‘update’ wizard - this is due to the designer only removing them from the CSDL but leaving the SSDL intact. The update wizard for some reason the considers them as “in the model” and ignores them.
There are a couple of workarounds for that - the one recommended by Microsoft in the MSDN forum for EF is to use an XML editor to manually clean up the SSDL.
In addition, my add-in toolkit for L2S and EF has a “cleanup” feature for entity framework models that will detect and allow you to either drop the SSDL or recreate the CSDL for “orphaned” entities.
The cleanup feature is described in more detail here:
http://blog.huagati.com/res/index.php/2008/11/27/tools-part-7-add-ins-how-to-deal-with-the-entity-framework-designer-and-orphaned-entities/
[Reply]