Connect to MySQL database using ABP entityframework

Source: Internet
Author: User

The ASP. NET boilerplate (ABP) is a very popular DDD framework under the platform, which has provided us with a large number of functions, which is very convenient for building enterprise applications.

The introduction of this framework I would not say, interested can see the official document: Http://www.aspnetboilerplate.com/Pages/Documents

Using Abp+ef+sql server is a relatively recommended combination, but since we are using EF, it should be separated from the database, which means we should be able to use other databases, such as MySQL.

The ABP Initialization project template also provides the module zero project, provides us with the user, the role, the privilege and so on common function, but when uses the initialization template connection MySQL to be able to error, cannot run, below I solves the abp+mysql question. Here are the steps to proceed:

1. Download the ABP project template from the official website and unzip it to local, with VS open, here we create a new project Connectmysql.

2. Set Xxx.web as the startup project, build this solution, make nuget download the related package.

3. Prepare a SQL Server database, modify the connectionstring of the Web. config database, and connect to the SQL Server database.

4. Open the Package Manager console window, select Xxx.entityframework as the default project, run the Update-database command, and the system will create the database and the corresponding table in SQL Server.

5. Open SSMs, connect to the new database in the previous step, select the Generate Script command, and select Schema and data in the advanced options to generate a script file that creates the table and populates the data.

6. Next is the cumbersome SQL Server script to MySQL script, I use notepad++, do a lot of batch replacement, the script to convert to MySQL support content.

In order to facilitate everyone, I directly put the converted SQL script out, you can directly run. Script download

7. Open MySQL Server, create a new MySQL database, and run the script prepared in the previous step.

8. We go back to VS, for xxx.entityframework and Xxx.web, add MySql.Data.Entity through NuGet:

9. Open Web. config for the project, because MySql.Data.Entity was added in the previous step, Web. config has added the relevant configuration for MySQL. We just need to modify the connection string, comment out the SQL Server string, and add a new connection string:

<add name= "Default" providername= "MySql.Data.MySqlClient" connectionstring= "server=localhost;port=3306"; Database=test;uid=root;password=xxx "/>

10. Open the Configuration object for the EntityFramework project, in the Migrations folder, modify the constructor, and specify the SQL generator that uses MySQL.

Public Configuration ()
{
automaticmigrationsenabled = false;
Contextkey = "Connectmysql";
setsqlgenerator ("MySql.Data.MySqlClient", New MySql.Data.Entity.MySqlMigrationSqlGenerator ());
}

11. Now let's try to see if you can use EF's code first for MySQL. We create a test entity student in Xxx.core:

public class Student:entity
{
[Stringlength (50)]
public string Name {get; set;}
Public DateTime Birthdate {get; set;}

public bool Gender {get; set;}
}

12. Locate the Connectmysqldbcontext in Xxx.entityframework and add the student application:

public class Connectmysqldbcontext:abpzerodbcontext<tenant, Role, user>
{
Todo:define an idbset for your entities ...
Public idbset<student> Students {get; set;}
/* Note:
* Setting "Default" to base class helps us if working migration commands on the package Manager Console.
* But it could cause problems when working Migrate.exe of EF. If you'll apply migrations on command line, does not
* Pass connection string name to base classes. ABP works either.
*/
Public Connectmysqldbcontext ()
: Base ("Default")
{

}

......

13. We compile this solution and then in the Package Manager Console window, enter the command add-migration addstudent, where addstudent is a command for our change. After the command finishes running, in the Migrations folder, you create C # code that upgrades the database.

14. Continue to enter the command update-database in the Package Manager Console window and the system will apply the database changes to our MySQL database.

15. We go to the MySQL database, refresh, you can see the system automatically created the students database:

16. Finally, we build the entire solution, run the website, can see our website is working properly.

Connect to MySQL database using ABP entityframework

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.