Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are essential differences between the different code generation items for EDMX model?

I'm trying to ramp up on the entity framework so I don't feel like I'm in the dark ages. I tried (and have thus far failed) to intuit from generated code what the essential differences between the available code generation items.

It seems POCO isolates the entity data structures from the ojbect that moves them in/out of a datastore.

I'm not sure what a "Self-Tracking Entity" is. I'm guessing the tracking part refers realizing the so called "unit of work" pattern, but I'm not posative. And more head scratchingly, I guess I'm wondering "self tracking as opposed to what?".

enter image description here

like image 428
Aaron Anodide Avatar asked Aug 19 '11 21:08

Aaron Anodide


People also ask

How is EDMX file generated?

edmx is basically an XML file which is generated when we added Entity Framework model. It is Entity Data Model Xml which contains designer (Model) and code file(. cs).

What is the difference between EF6 and EF core?

EF 6 is a stable and mature ORM while EF Core is relatively new. Microsoft rebuilt EF Core from the ground up and removed many of the internal dependencies and providers that EF 6 had (like SQLClient). In the long run, that will make EF Core much more extensible and lighter weight.

What is the difference between code first and database first?

In the code first approach, the programmer has to write the classes with the required properties first, while in the database first approach, the programmer has to create first the database using GUI.

Which option in EDMX can be used to get the latest changes from the database?

Use the update model wizard (to update the storage model), open the . edmx file using the XML editor, find the desired property in the CSDL (conceptual model) section and change the desired attributes. This is basically the same as option 1, but you're editing the XML directly (a find and replace might be useful here).


1 Answers

POCO Generator

POCO stands for Plain Old C# (or CLR) Object. POCOs are independent on EF. They are just classes following some rules but you can inherit them from your own type if you want. They also don't include any persistence dependent data.

Currently this type is most popular because it is trend of current architecture approaches to have everything POCO and lightweight. In some situations using POCOs is more complex but that is a price for persistence ignorant architecture.

EntityObject Generator

This generator produces the same type of entities as the default code generation method for EDMX. These entities derive from EntityObject class which makes them fully dependent on Entity framework (I call them heavy entities). This dependency offers them some additional features or simplifications but it makes them harder to use in detached scenarios and their usage leads either to architecture with tight coupling of upper layers to Entity framework or to additional complexities when achieving better separation.

This type of entities was the only type supported in the first EF version. Even everybody is using POCOs to achive better separation this type is native for EF and probably offers most features.

This generator also makes your entities serializable (with DataContractSerializer).

Self tracking entities (STE) Generator

This is very special type of POCO generator. When working with EF we differ two scenarios. Attached scenarion where EF tracks changes made to entity and detached scenario where you did changes outside of EF scope and once you attached entity to EF you must tell it what changes you did. Typical detached scenarios are web services where you pass entities to the client and once the client passes them back you must somehow synchronize changes so that EF knows what SQL commands it must generate. STEs are for these detached scenarios. They are implementation of change set pattern = they track their current state as well as changes made since self tracking started (as old DataSet did).

This is a theory. In the real world STEs have some big disadvantages and are suitable only for very specific scenarios.

Edit:

There is one more generator which is installed together with Entity Framework 4.1.

DbContext Generator

This generator leads to the same entities as POCO generator. The only difference is used API. POCO generator uses ObjectContext API whereas DbContext generator uses POCOs with DbContext API (only available in EF 4.1 and June 2011 CTP). The difference between these APIs is matter of choice.

like image 116
Ladislav Mrnka Avatar answered Oct 09 '22 12:10

Ladislav Mrnka