what is the advantage of using code based configuration in asp.net mvc4

Posted by balajirnaukri-12656 under ASP.NET MVC on 8/21/2013 | Points: 10 | Views : 3555 | Status : [Member] | Replies : 1
what is the advantage of using code based configuration in asp.net mvc 4

Thanks and Regards
ASP.NET Devloper
Solve-IT corp


Posted by: Kmandapalli on: 8/22/2013 [Member] Silver | Points: 25


Configuration for an Entity Framework application can be specified in a config file (app.config/web.config) or through code. The latter is known as code-based configuration.

The config file takes precedence over code-based configuration. In other words, if a configuration option is set in both code and in the config file, then the setting in the config file is used.

Using DbConfiguration:
Code-based configuration in EF6 and above is achieved by creating a subclass of System.Data.Entity.Config.DbConfiguration.
The following guidelines should be followed when subclassing DbConfiguration:
* Create only one DbConfiguration class for your application. This class specifies app-domain wide settings.
* Place your DbConfiguration class in the same assembly as your DbContext class. (See the Moving DbConfiguration section if you want to change this.)
* Give your DbConfiguration class a public parameterless constructor.
* Set configuration options by calling protected DbConfiguration methods from within this constructor.

Following these guidelines allows EF to discover and use your configuration automatically by both tooling that needs to access your model and when your application is run.

A class derived from DbConfiguration might look like this:

using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.SqlServer;

namespace MyNamespace
public class MyCofiguration : DbConfiguration
public MyConfiguration()
SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0"));

This class sets up EF to use the SQL Azure execution strategy - to automatically retry failed database operations - and to use Local DB for databases that are created by convention from Code First.

Moving DbConfiguration:
There are cases where it is not possible to place your DbConfiguration class in the same assembly as your DbContext class. For example, you may have two DbContext classes each in different assemblies. There are two options for handling this.

The first option is to use the config file to specify the DbConfiguration instance to use. To do this, set the codeConfigurationType attribute of the entityFramework section. For example:

<entityFramework codeConfigurationType="MyNamespace.MyDbConfiguration, MyAssembly">
...Your EF config...

The value of codeConfigurationType must be the assembly and namespace qualified name of your DbConfiguration class.
The second option is to place DbConfigurationTypeAttribute on your context class. For example:
public class MyContextContext : DbContext

The value passed to the attribute can either be your DbConfiguration type - as shown above - or the assembly and namespace qualified type name string. For example:
[DbConfigurationType("MyNamespace.MyDbConfiguration, MyAssembly")]
public class MyContextContext : DbContext

Mark as Answer if satisfied.............

Shree M.

Kavya Shree Mandapalli

balajirnaukri-12656, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response