Back to Catalog
Connect MySQL in. NET Frameworks EF We've passed the test, and in Dotnet core efcore to connect MySQL we need to test, and in the test process has some problems, of course, the final is solved, the following summary, Share to everyone!
- MySQL Project's dependency package
- Data context and connection string
- Data warehousing
- Add a module extension
- Business Layer Injection
- Business implementation
MySQL Project's dependency package
- Microsoft.entityframeworkcore
- MySql.Data.EntityFrameworkCore
Data context and connection string
For the context of MySQL and the use of SQL is no different, you need to be aware of the SSL to add the negation, or there will be an exception
MySql.Data.MySqlClient.MySqlException:The host localhost does not a support SSL connections.
Public Partial classMysqlerpcontext:dbcontext, Ierpcontext {protected Override voidonconfiguring (Dbcontextoptionsbuilder optionsbuilder) {optionsbuilder.usemysql (@"Server=localhost;database=ef;uid=root;pwd=root; Sslmode=none"); Base. Onconfiguring (Optionsbuilder); } PublicDbset<system_users> System_users {Get;Set; } PublicDbset<user> Users {Set;Get; } }
Data warehousing
Implement the efrepository in the inheritance infrastructure, we need to pass a data context for warehousing, which is the MySQL context object defined above, so your repository can manipulate the context.
Public class where class { publicbase(new Mysqlerpcontext ()) {} }
Add a module extension
Our infrastructure has a number of features that have been implemented, and we will be able to extend the method to facilitate the use of business systems, and business-related objects, such as business warehousing, business contexts can be added in the business system extensions, methods of post-injection work, general business extension code is as follows
/// <summary> ///Modular Extensions for the current project/// </summary> Public Static classmoduleextensions {/// <summary> ///register a data Warehouse/// </summary> /// <param name= "Configuration" ></param> /// <returns></returns> Public StaticModulemanager Useerprepository ( ThisModulemanager Configuration) {configuration. Registergenericmodule (typeof(irepository<>), typeof(erprepository<>)); returnconfiguration; } /// <summary> ///registering a data context/// </summary> /// <param name= "Configuration" ></param> /// <returns></returns> Public StaticModulemanager Useerpcontext ( ThisModulemanager Configuration) { configuration. Registermodule<ierpcontext, mysqlerpcontext> (); returnconfiguration; } }
Business Layer Injection
After the extension of the module is implemented, it is injected into the business system when it is initialized, which method is used to register where, generally in global or startup to implement the injection function.
// registering the module modulemanager.create () . USEAUTOFAC () . USEESBIOC () . Useerpcontext () . Useerprepository ();
Business implementation
The corresponding storage can be removed directly from the module, and then the corresponding curd operation will be performed.
Modulemanager.resolve<irepository<user>> (). Insert (new api.user { "The" });
So we in dotnet core through the Efcore to operate the MySQL database is completed, it should be noted that in the MySQL connection string, be sure to add sslmode=none This property, otherwise the SSL link will be enabled!
Thank you for reading!
Back to Catalog
Dotnetcore Cross-platform ~efcore connection to MySQL