Asp. NET no magic--asp.net MVC and database entityframework configuration and connection string

Source: Internet
Author: User

The previous articles described how to use the Entity Framework to manipulate databases, but there are still some questions about the configuration of EF, the designation of connection strings.
The configuration of EF is described in this chapter.
There are two ways in which EF can implement configuration, namely code mode and configuration file.

configuration files for the Entity framework

For a profile, the following is automatically inserted in the configuration file when the Entity Framework is installed:

    

The first is configsection:
Configsection is a. NET program used to customize the nodes of the configuration node, so the purpose of this node is to tell the. NET program to have a custom configuration node, the name and type of the custom node.
The name is well understood to be the next entityframework node, but what does the type refer to?
Type refers to the processor that handles the configuration information referred to by the name, and the following code is the node type definition configured above:

  

The content inside is related to the configuration file, and how to customize the configuration node can be consulted: https://msdn.microsoft.com/en-us/library/2tw134k3.aspx

entityframework Node parsing

Under the EntityFramework node, there are providers, interceptors, contexts and defaultconnectionfactory, which are basically corresponding to the entityframeworksection type.

1. Providers:

According to the type entityframeworksection definition, providers is a provider collection, and each provider configuration requires provider name (invariant for identity), Type (the namespace and assembly where the provider resides). The following is the default SQL Server provider:

  

2. Interceptors:

Interceptors, also a list, each interceptors provides the type (namespace and assembly name) along with the corresponding parameters to complete the configuration, and the following is the log interceptor that comes with EF.

  

The following are the effects of adding log interceptors:

  

Note: The so-called interceptor is a facet-oriented (AOP) programming approach, which is a way to extend the application without modifying the source code. General AOP is used to handle logging, performance statistics, security controls, transaction processing, exception handling, and other functions that do not modify the original business data.

3. Contexts:

There can be more than one context node under the contexts node, which is used for database initialization, and by default DbContext is automatically created if no database is initialized. If you do not need to be able to add the following configuration in the configuration file is disabled:

  

You can use the Databaseinitializer element to set up database customization initialization (here is an example of an official document):

  

Or the automatic migration of the database (Official document example):

  

4. Defaultconnectionfactory

Defaultconnectionfactory will specify a default connection factory, which will be used when code first is used to find a database for dbcontext use (note: The DbContext does not take effect until the database connection string is present).
When installing the EF package via NuGet, a default Connectionfactory,sql Express or LOCALDB is registered according to the local conditions(note: So a new MVC-authenticated template project has a data storage mechanism, This mechanism may be implemented with SQL Express or LocalDB. The following configuration is based on LocalDB, mainly with factory types and parameters

  

If Defaultconnectionfactory is not set then the default is to use Sqlconnectionfactory ( Note: The SQL Server database is used if it is not configured ).

Sqlconnectionfactory can also be configured in Defaultconnectionfactory, which can specify a connection string by parameter, so that the connection string is not required:

  

about ConnectionString

About ConnectionString The defaultconnectionfactory is not present in the database connection string, the time to take effect, then how to handle the database connection string? DbContext how to tell if the connection string exists? The following is the logic of connection string selection:
1. When no configuration any connection string ( Includes the default connection factory and the ConnectionString node), Dbcontext When using the parameterless construction method , DbContext will use the DbContext namespace and the DbContext type name as the database name by default (as in this case, Blogrepository.blogdbcontext), and then create a connection to the SQL Express or LocalDB database connection prior to use.
note: Both SQL Express and LocalDB can be thought of as a developer version of the SQL Server database.
2. In the construction method of DbContext Specifies the database name ( note must be specified in the construction of the base type), then the specified name will be used. Then create a connection to the SQL Express or LocalDB database connection prior to using the former.

3. Specify the database connection string in the DbContext constructor method (note that it must be specified in the construction of the base type), and the string will be used. And by default, System.Data.SqlClient is used as the provider, that is, specifying the connection string by default using SQL Server, if you need to change the context to use a different database. The settings for the database.defaultconnectionfactory.
4. If the connection string is configured in the Web.config/app.config file, the type name of the DbContext is the same as the name property of the connection character (regardless of whether the full namespace is included) or the name of the connection string is specified in the base class construction method, the configuration information is used as the connection string.
For more information: https://msdn.microsoft.com/en-us/library/jj592674 (v=vs.113). aspx

modifying configurations using code

The above mainly introduces the configuration of provider provider, Interceptor Interceptor, default connection factory Defaultconnectionfactory and database initialization context in EF, This can be done in code to complete the configuration, but note that the configuration of the code is overwritten by the configuration file configuration, if the same configuration exists in the code and configuration file, then the configuration file takes effect .
1. Define a type to inherit to System.Data.Entity.DbConfiguration.
2. Create an parameterless public construction method for this type.
3. Call a series of protection methods in the construction method to complete the configuration.

  

4. Use the Dbconfigurationtype feature on the DbContext type:

  

Note: The connection string database name here needs to be passed in through the DbContext base class, otherwise the {namespace is used. DbContext} name as the database name.

5. Remove all EF-related configurations and run the program:

  

Choose the right configuration according to the actual requirement

In this example, because there are multiple database solutions, there will be multiple sets of configurations, and they all share the same MVC program, so each time you switch the database when you put the configuration file in Web. config, you need to modify the corresponding EF configuration, so you should try to write the immutable configuration in the program .
The interceptor should be written in the configuration file as it may be arbitrarily added and deleted.
Second, the database connection string is often changed, such as environment changes, database account password changes, etc., so should also be placed in the configuration file.
According to the above analysis of the My blog program modified as follows:
1. Define the provider in the configuration type:

  

2. Add the EF configuration to the Web. config file, but only the interceptor is configured:

  

3. Add a database connection string:

  

4. Use the Dbconfigurationtype feature on the DbContext type:

  

5. Operation Result:

  

Summary:
This chapter focuses on the configuration of EF, which implements the EF configuration in the same way as configuration files and code, and when you use code configuration, the MVC project no longer needs to join any EF configuration to run the application. Understanding the configuration file helps you understand how EF works and makes it easier to change the database to MySQL.
In the Microsoft documentation, there is this sentence:
Entity Framework allows a number of settings to is specified from the configuration file. In general EF follows a ' convention over configuration ' principle. All the settings discussed in this post has a default behavior, you are need to worry about changing the setting when th e default no longer satisfies your requirements.
This means that EF follows the Convention precedence configuration principle, and all configurations have default behavior, generally without the need to modify the configuration if there is no special requirement.

Reference:

https://msdn.microsoft.com/en-us/library/jj556606 (v=vs.113). aspx
https://msdn.microsoft.com/en-us/library/jj680699 (v=vs.113). aspx
https://msdn.microsoft.com/en-us/library/jj592674 (v=vs.113). aspx

This article connects: http://www.cnblogs.com/selimsong/p/7655949.html

Asp. NET no magic--Directory

Asp. NET no magic--asp.net MVC and database entityframework configuration and connection string

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.