Thursday, 20 November 2014

What’s New in Entity Framework 5.0?

The performance of the Object Relational Mappers

In the world of Object Relational Mappers, there is one topic that have always been on the table: Performance.
We have automatic configuration, generation and mapping. We have LINQ to Entities doing a great deal of the work for us, including generating the SQL and allowing us to use the built-in CLR functionality in order to produce working queries relatively easily. 
The main problem, of course, is that all of this goodness comes with a price in terms of performance. All object relational mappers produce a well-known overhead and many companies, which are developing high-performance applications, ask themselves: “Is it worth it? Aren't they too slow or too heavy?”
The answer is, "Yes, they are". But that’s why we, the engineers, are here – to make it work faster and keep the benefits measured in production time. If you just install Entity Framework and start it, you’ll certainly need to make a nice refactor sooner or later.

Auto-compiled queries

Getting straight to the point, Entity Framework 5.0 uses the so-called “auto-compiled” queries feature which compiles (read “translates”) the Entity SQL or LINQ to Entities expression tree in a pure SQL (or T-SQL) code. The question is "Are these generated queries actually effective ?". 
In order to do some accurate measurements, we need to get few things straight:
  • Don’t look at the very first query sent.
    During that query, the framework “warms up” and configures the views and all other components it needs.
  • Next, don’t look at the first invocation of a given query.
    During that time, EF caches parts of the query, so the subsequent calls can be faster. This doesn’t mean the query is compiled. It’s not…yet.
  • Take into consideration that .NET 4.5 is an “in-place” upgrade
    This means that once it is installed, you will not have any indications that the new version is actually installed (no GAC folder, no 4.5 assemblies, no registry entry). You also can’t go back to .NET 4.0 so even if you target 4.0 in the project properties, it will still use the 4.5 run-time.

Few performance tests

I've created a  quite slow query (few joins, dozens of filters and ternary operators) and measured its performance using .NET 4.0 with Entity Framework 4.0 (on another machine, of course), and .NET 4.5 with Entity Framework 5.0.
The results were the following (tested with 4 core CPU and 8GB of RAM, Windows 7, and SQL Server 2008):
Without auto-compiled query – 40ms
With auto-complied query – 10ms
With manual use of the CompliedQuery class – 5ms
This means that you are indeed going to get some out-of-the-box performance benefits by simply installing EF5 and .NET 4.5. But if you want faster query generation, use the CompiledQuery class instead, but be prepared to change your current DAL architecture to support this.
Unfortunately, I no longer have the sources of the examples I've used. 

Enum Support

There is support for enumerations in the EF 5.0 release. In order to activate it, create a scalar property of structureInt32 in the .edmx, right-click on it, and select “Convert to Enum”.
EF5-enum
This will create an Enum which, upon DDL generation, is translated into a simple column of type Int. This feature provides additional abstraction and integrity constraint in the DAL layer, but from a database point of view, there is no way to find out what a given value in a table really means. You will need to check the project code for that, or simply remember it.

Spatial Data Types

Another interesting feature is the support fo spatial data types in SQL server. For those who are not aware of them, these types are geography and geometry related classes which allow us to work directly with such data inside the SQL Server (like geographic locations or geometrical representation). Below are the related available CLR types. The blue boxes in the following diagram are concrete classes.
EF5-spatial-data-types
There is also a built-in spatial result view in SQL Server, which looks like this:
EF5-spatial-result-view
It is actually quite impressive. For example, if you have a table with points (longitude and latitude) of office locations, for example, you can visualize them directly in SQL Server. You can also utilize a number of functions, such as calculating the distance between two points. With Entity Framework 5.0, we can do it in C# (or any CLS supported language, for that matter). Note that there are still too few resources on the internet related to this feature, so prepare to dig if you plan to use it.

Code First now works with LocalDB

The Entity Framework Code First approach now works with LocalDB. For those of you who haven’t heard aboutLocalDB, think of it as a hybrid between SQL Server Express and SQL Server Compact. It runs in a child-process, rather than in a full-blown Windows service. It’s the default Visual Studio 2012 development server, and it’s meant to be light and easy to configure.

DbContext is now the default generated context

DbContext is not something new. A wrapper around ObjectContext, generated with the help of T4 templates, utilizing the convention-over-configuration principle of the POCO classes. It generates something like this:

And the entities look like this:
As you can see, we are talking about pure POCO classes here, but that doesn’t mean that we are actually working with these simple classes at run-time. In fact, dynamic proxies are being generated every time you use the class, enabling lazy initialization and change tracking. 

Multiple diagrams per model

The new Visual Studio 2012 designer supports multiple diagrams per model. We all know how big models are maintained. Huge databases can quickly become unmanageable without the use of the proper tools.
EF5-VisualStudio-2012
Here is a screenshot of the standard designer screen in Visual Studio 2012. You can see multiple diagrams on the upper right-hand corner. On the context menu you can select “Move to new Diagram” or “Include” related, which will include all entities with relations to the selected one. You can also change the color if you want. 

Table-valued functions

Table-valued functions are also not new in SQL Server, but until now there was no built-in support in Entity Framework. For those of you who are not familiar with these type of functions, they are somewhat of a hybrid between a view and a stored procedure. They support procedural code inside parameters, but can be called from a SELECT statement. They return a single value, but this value is actually a table. This means that it can be chained in a LINQ-to-Entities query.
Here is a simple DDL script for a table-valued function in SQL:
which can be used in LINQ-to-Entities like this:

Batch import of stored procedures

The last feature I want to mention is the ability to import a batch of stored procedures/functions into the .edmx model. Not much to write about here as it’s a matter of click-and-go.

entity-data-model-wizard

What about Entity Framework 6.0?

Actually, the alpha version of Entity Framework 6.0 is now available in NuGet. You can download it from here if you like.
Here is a list of pre-announced features:
Task-based Async – Allowing EF to take advantage of .NET 4.5 async support with async queries, updates, etc.
Stored Procedures & Functions in Code First – Allow mapping to stored procs and database functions using the Code First APIs.
Custom Code First Conventions – Allowing custom conventions to be written and registered with Code First.

4 comments:

  1. New Features in Entity Framework 4

    Entity Framework 4 was actually second version but named as EF 4 to align with .NET Framework v4.0 and Visual Studio 2010 having the following new features including all major and minor versions of Entity Framework i.e. 4, 4.1, 4.1.1, 4.2, 4.3 and 4.3.1.

    Support for POCO – Plain Old CLR Objects that decouples it from any persistence technology.
    Lazy Loading – On demand loading of related entities.
    Code Generation – Customizable code generation templates.
    Support for Model First Development – We first create model of entities and later on generate script for corresponding database tables and relations using Visual Studio 2010.
    DbContext API provides simplified abstraction over ObjectContext.
    Code First Development – Conceptual model can be created by writing classes.
    Code First Migration facilitates incremental migration of database created by code first development.

    ReplyDelete
  2. New Features in Entity Framework 5

    Entity Framework 5 targets both .NET 4 as well as .NET framework 4.5 with following new features.

    Enum Support – enum properties supported in entity classes with EF5.
    Spatial Data Types – support for working with Spatial data using DbGeography and DbGeometry classes.
    Support for Table-Valued functions of SQL Server that are just like stored procedures with one key difference and that difference is TVF result can be used in a LINQ query.
    Performance Enhancements – around 65% to 70% performance improvment over Entity Framework 4.

    ReplyDelete
  3. New Features in Entity Framework 6

    This version of Entity Framework is available with Visual Studio 2013 but we can download EF 6 for Visual Studio 2012 too.

    Code-based configuration support along with traditional XML-based configuration support.
    Most exciting feature of Entity Framework 6 is asynchronous support i.e support for asynchronous queries and save. Asynchronous feature is introduced in .NET 4.5. WCF 4.5 also supports asynchronous programming.
    Logging Enhancements – logging of database commands helps to understand what’s going on while interacting with data.
    Support for stored procedure mapping to different operations like insert, update, delete.
    Testability Improvements.
    Custom Code First Conventions helps to customize default conventions.
    The post discussed key and interesting new features of Entity Framework but for a complete list of new features and enhancements about this technology, please visit Microsoft Entity Framework Documentation.

    ReplyDelete
  4. Top 10 Features of Entity framework 4.0

    POCO and Persistence Ignorance
    Define your own objects that are completely decoupled from persistence concerns.

    T4 Code Generation
    Create customized code generation templates (for example, POCO classes).

    Lazy Loading
    Load related entities automatically on-demand.

    Change-Tracking
    Different options for tracking changes to entities.

    N-Tier Support
    Propagate entity changes across service boundaries for batch updates.

    Pluralization
    The tools fix up entity names in case database tables are in plural form (Northwind!).

    Model-First
    Create an entity model and generate the database from it.

    Code-Only
    Create classes without a model.

    Entity Model Designer Improvements
    Have more control over storage, conceptual and mapping schemas.

    Miscellaneous Improvements
    a. Complex Type Support

    Create complex types in your model.

    b. Foreign Keys

    Optionally include foreign keys in your model.

    c. LINQ to Entities Functions and Operators

    Call database, conceptual and model-defined functions from L2E queries.

    d. Testability Improvements with IObjectSet

    Test-driven development is easier now.

    e. T-SQL Readability and Performance

    ReplyDelete

Angular Tutorial (Update to Angular 7)

As Angular 7 has just been released a few days ago. This tutorial is updated to show you how to create an Angular 7 project and the new fe...