Asp. NET no magic--asp.net MVC and database Mysql&ef

Source: Internet
Author: User
Tags mysql connection string

This chapter describes how to use my SQL through the Entity Framework, which describes the provider configuration item before introducing EF. Also introduced in the ASP. NET access MySQL data is still through the ADO, different places just replaced MySQL data provider, then in the EF can also only need to replace the "provider" it? Let's take a look at how the Entity Framework connects to using my SQL database.

1. Install the Entity Framework components through nuget onto the Blogrepository.mysql project:

  

2. Introduction of MySql.Data.Entity.EF6 in the Blogrepository.mysql project:

EF is a major component that must be installed, except that the EF contains only SQL Server related components that do not contain MySQL, so there are two ways to install and introduce MySql.Data.Entity.EF6, one of which is to install the MySQL component connector/ NET, which is then found in the reference manager:

  

Another way is to install it through the NuGet Package Manager:

  

3. Create a DbContext:

  

4. Use the Mysqlefconfiguration type on the DbContext type:

  

It is officially recommended to use the Mysqlefconfiguration type because all of the MySQL type-related components are added to the type, or adddependencyresolver is called in the custom configuration type (new The Mysqldependencyresolver ()) method is added by itself and the added components are:

DbProviderFactory-Mysqlclientfactory
Idbconnectionfactory-Mysqlconnectionfactory
Migrationsqlgenerator-Mysqlmigrationsqlgenerator
Dbproviderservices-Mysqlproviderservices
Iproviderinvariantname-Mysqlproviderinvariantname
Idbproviderfactoryresolver-Mysqlproviderfactoryresolver
Imanifesttokenresolver-Mysqlmanifesttokenresolver
Idbmodelcachekey-Mysqlmodelcachekeyfactory
Idbexecutionstrategy-Mysqlexecutionstrategy

  Of course, you can also customize a configuration type, the replacement provider mentioned earlier in the article:

  

Apply this custom type to the Dbconfigurationtype attribute in the same way:

  

4. Modify blogrepository to use SQL instead of EF:

  

5. Add the MySQL connection string:

  

The DbContext type automatically finds and uses a connection string with a name that is consistent with its type name in the ConnectionString node of the configuration file.

Note: The name is consistent with the DbContext type and providername is MySql.Data.MySqlClient, which is inconsistent with SQL Server and requires a specific program to parse and use the connection string.

6. Run the program:

  

  

Note: normal operation is because the mysqlefconfiguration type is used in the DbContext type, The type is located in the MySql.Data.Entity.EF6.dll assembly, so the main project compiles when the assembly is included, without the absence of an assembly.

Summary:

This chapter describes how to use the MySQL database with EF, and the difference between SQL Server is that you need to introduce MySQL-related assemblies and then use the MySQL data provider so that EF can operate the MySQL database. This substitution is done by using the Dbconfigurationtype attribute on the DbContext type.

With a few introductions to ASP. NET Access database and EF usage, you may find some questions about the configuration of EF:

0 configuration information is created dynamically when the EF component is installed, but these configurations are not available.

0 You can use the form of code (the Dbconfigurationtype attribute) to specify the configuration.

The 0DbContext connection character can be passed in through the base class construction method.

The 0DbContext connection string can read the configuration file in its corresponding type name.

The above questions will be presented in the next article.

Reference:

Https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html

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

Asp. NET no magic--Directory

Asp. NET no magic--asp.net MVC and database Mysql&ef

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.