Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about generators for Entity Framework 4.1

I'm evaluating Entity Framework for a project against a legacy database. The database is rather well designed and it has already been decided that we would use the Database-First approach. The application would be WinForms based but I would like to plan ahead and take ASP.Net into account as well as it would likely be requested by management at some point and I would like to reuse the DAL.

Now, I understand that Entity Framework provides several ways for the entity objects to be generated:

  • Objects derived from EntityObject
  • POCO (ObjectContext)
  • POCO (DbContext)
  • Self-Tracking Entities (STE)

I've been trying to compare the three approaches and I've found out this so far.

The EntityObject T4 template is the richest of them all. For example, it puts the comments from the Entity Model into XMLDoc comments on top of each class and each property of the entity, which is rather nice and useful in terms of code maintainability. None of the other generators support this out of the box (although I understand it is possible to modify the T4 templates, but it obviously requires some work). On the other hand, the DbContext API is somewhat nicer.

EntityObject derived types provide an event that you can hook to when an entity's property changes, which is useful to implement business logic.

I understand that POCO is more natural for this, but the T4 generator will overwrite all my changes every time the model changes. Sure, generated POCOs are partial classes but to my knowledge, it is not possible to override a property already defined in a partial class, so I am unsure how implementing business logic in them is getting done?

A lot of people seem to hate on the EntityObject derived classes, preferring the POCO-approach, but it seems I'm losing a lot of useful features that are provided out-of-the-box by EntityObjects, by going the POCO route. Considering the general strong preference on POCO objects, I may be misunderstanding something hence my question.

Last, which generator is the best-suited in an ASP.Net environment? I'm worried about my entities changes stopping being tracked? Are Self-Tracking Entities more useful in this case or do they have only value with web-services (which I likely won't be using)?

Many thanks in advance.

like image 761
Kharlos Dominguez Avatar asked Oct 11 '11 11:10

Kharlos Dominguez


1 Answers

If you want to use EF 4.1 your options is only DbContext Generator. All other generators (POCO, EntityObject and STEs) are for ObejctContext API (EF 4.0). I described differences between generators here.

In your case EntityObject generator can be used for WinForms but it will be cumbersome for ASP.NET solution where POCOs are better because ASP.NET application is detached scenario - you will have to deal with change tracking in ASP.NET anyway.

The purpose of STEs is described here but they are not very useful in WinForm where you can use attached entities directly or ASP.NET where it demands to store them between requests in view state.

Any business logic you want to put into entity directly must be coded in T4 generator otherwise it will be overwritten after each generation. Every feature of EntityObject based generator you mentioned can be implemented in POCO / DbContext generator as well.

like image 143
Ladislav Mrnka Avatar answered Sep 27 '22 20:09

Ladislav Mrnka