In this PDC session, the ADO.NET team showcased new features in Entity Framework 4.0.
Model First Capability
Entity framework 4.0 has the ability to generate a database from an entity framework model. Just right-click on the entity model canvas and choose “Generate Database” – this would generate and add an SQL script to your project which you can easily modify, add indexes etc.
Pluralization of Entity Set Names
The Pluralization of entity set names works wonderfully well, including some English language exceptions which you wouldn’t usually expect (e.g. Goose – > Geese etc.).
Entity references are now lazy loaded as they are accessed. Cool!
Using POCO Objects
It is now possible to use POCO objects instead of the code generated ones, and it is very easy to so using the new IObjectSet interface. Adding lazy loading to POCO objects is also a breeze – just make your properties virtual and set a flag in the context – and you’re done!
Code Generation Templates
It is now easy to customize the templates used by the Entity-Framework code generation via T4 templates. With this, you can generate POCO objects or any other implementation that fits your needs. Just use one of the shipped templates or create your own.
For example, you can have your context implement an interface of your choice and thus very easily implementing the Repository pattern and creating true unit-tests which don’t even hit the database on top of entity framework.
EF 4.0 contexts now have methods for easily controlling the state of an entity (modified, added, deleted etc) – “ChangeObjectState()” and “ChangeRelationshipState()”.
Even better, the Entity Framework Feature CTP also contains a “self-tracking entity template” which makes creating self-tracking entities a piece of cake. This template is currently available in the feature CTP 2 and will be shipped as part of the release of NET 4.0.
SQL Generation Improvements
A lot of improvements were made to the SQL generation engine which generates the SQL queries from LINQ queries, making them more readable, efficient and DB friendly.
Use Entity Framework as a Pipe to the DB
Entity framework contexts now support easily executing custom SQL queries and stored procedures on the DB on which entity framework context operates on. This might be useful for those small occasions on which you need to perform some unique operations which just don’t fit in the entity model.
Foreign Keys Now Automatically Appear in the Entity Model
When generating a new model from a database, Entity Framework now automatically adds a member to entities representing their foreign key references. This makes data bindings and future queries much more easier to do.
Improved integration between Entity Framework and WPF
Building WPF applications against an entity framework model just got a lot easier as a lot of the required code which was previously required to write by hand is now automatically generated, allowing to easily create smart clients with drag-and-drop development and data binding.
Code Only Entity Model (Feature CTP)
The “ContextBuilder<T>” class can be used to create an entity model dynamically on the fly in a code only manner, create the backing DB automatically and get to work. Just create your entity model via code during runtime and change it according to some external circumstance. Amazing!