Configuring Entity Framework 6.0.X for Services That Use Windows Azure SQL Database

January 5, 2014 — Leave a comment

ef6 Since Entity Framework 6 has achieved General Availability I have been waiting for version 6.0.2 (available on NuGet). This version of Entity Framework packs a ton of new features, including async queries and built-in retries to deal with the inevitable transient faults. On Windows Azure, there are to be expected. Having the retry logic built-in is music to any seasoned Windows Azure developer who has had to implement this logic using the Transient Fault Handling Application Block.

Don’t get me wrong, the Transient Fault Handling Application Block is still very useful to implement your own error handling strategies. A good example can be found in my post about defining an HTTP Transient Error Detection Strategy for REST calls.

Since Entity Framework has changed a lot since version 5.0, it may come as a surprise that the transient fault retry logic isn’t activated by default. If you want to take advantage of this feature, you will need to activate it. The following piece of code, will show you what you need to know about configuring Entity Framework 6.0.X for services that use Windows Azure SQL Database.

Create a subclass of System.Data.Entity.Config.DbConfiguration and configure your Entity Framework content from its constructor.

public class ContextConfigurations
    : DbConfiguration
    public ContextConfigurations()
        SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());

This configuration class will get loaded automatically by both the tooling that needs to access your model and by your application . Be sure to create only one DbConfiguration class for your application. This class specifies app-domain wide settings. Furthermore, the configurations subclass needs to be placed in the same assembly as your DbContext class.

Setting DbConfiguration explicitly can be done by calling the static DbConfiguration.SetConfiguration method during application startup.


Notable New Features in Entity Framework 6

Async Query & Save (EF Designer & Code First)
Asynchronous query and save using the async and await keywords.

Connection Resiliency / Retry Logic (EF Designer & Code First)
Automatically retry any commands that fail due to connection breaks .

Logging and Intercepting Database Operations (EF Designer & Code First)
Anytime EF sends a command to the database this command can be intercepted by application code .

Code-Based Configuration (EF Desigener & Code First)
Code-based configuration is achieved by creating a subclass of DbConfiguration .

Custom Code First Conventions (Code First only)
Write your own conventions to help avoid repetitive configuration. For more advanced scenarios you can use Model-Based Conventions.

Code First Insert/Update/Delete Stored Procedures (Code First only)
Configure EF to use stored procedures to update data.

Testing with a Mocking Framework (EF Designer & Code First)
Create test doubles using a mocking framework (such as Moq).

Testing with Your Own Test Doubles (EF Designer & Code First)
Write your own in-memory implementation of your context and DbSets .

Working with Transactions (EF Designer & Code First)
Learn how to control the use of trasactions with Entity Framework.

Connection Management (EF Designer & Code First)
Learn how to control connection open/close and work with existing database connections.

Customizing the Migrations History Table (Code First only)
Customize the definition of the __MigrationHistory table.

Custom Migrations Operations (Code First only)
Create additional operations to be used in your code-based migrations .

Dependency Resolution (EF Desigener & Code First)
Lower level building blocks that enable the Code-Based Configuration feature.

No Comments

Be the first to start the conversation!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s