Dotnetcore Cross-platform ~efcore connection to MySQL

Source: Internet
Author: User
Tags dotnet mysql connection string mysql in

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!

    1. MySQL Project's dependency package
    2. Data context and connection string
    3. Data warehousing
    4. Add a module extension
    5. Business Layer Injection
    6. Business implementation

MySQL Project's dependency package

    1. Microsoft.entityframeworkcore
    2. 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

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.