DotNetCore cross-platform ~ How to connect EFCore to Mysql, dotnetcoreefcore

Source: Internet
Author: User

DotNetCore cross-platform ~ How to connect EFCore to Mysql, dotnetcoreefcore

Back to directory

In. net frameworks ef to connect to mysql we have passed the test, and in dotnet core to connect to mysql we need to test, and there are some problems in the test process, of course, this is the final solution. Let's summarize it and share it with you!

Mysql project dependency package

Data context and connection string

There is no difference between mysql context and SQL usage. You must note that the SSL denial to be added; otherwise, exceptions may occur at the same time.

MySql. Data. MySqlClient. MySqlException: The host localhost does not support SSL connections.

    public partial class MySqlERPContext : DbContext, IERPContext    {        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)        {            optionsBuilder.UseMySQL(@"Server=localhost;database=ef;uid=root;pwd=root;SslMode=None");            base.OnConfiguring(optionsBuilder);        }        public DbSet<system_users> system_users { get; set; }        public DbSet<User> Users { set; get; }    }

Data Warehousing

Implement the inheritance of EFRepository in the infrastructure. We need to pass a data context for the warehouse, that is, the Mysql context object defined above, in this way, your warehouse can operate on this context.

  public class ERPRepository<T> : EFRepository<T> where T : class    {        public ERPRepository() : base(new MySqlERPContext()) { }    }

Add module Extension

We have implemented some functions in our infrastructure, and we will make expansion methods to facilitate the use of business systems and business-related objects, such as business warehousing, the business context can be added to the Business System for extension. The general business extension code is as follows:

/// <Summary> /// modular extension for the current project /// </summary> public static class ModuleExtensions {// <summary> // register a data warehouse /// </summary> /// <param name = "configuration"> </param> /// <returns> </returns> public static ModuleManager UseErpRepository (this ModuleManager configuration) {configuration. registerGenericModule (typeof (IRepository <>), typeof (ERPRepository <>); return configuration ;} /// <summary> /// register a data context /// </summary> /// <param name = "configuration"> </param> /// <returns> </returns> public static ModuleManager UseErpContext (this ModuleManager configuration) {configuration. registerModule <IERPContext, MySqlERPContext> (); return configuration ;}}

Service Layer Injection

After the extension Implementation of modules, they are injected during business system initialization, and the method to be implemented is registered. Generally, the injection function is implemented in global or startup.

// Register the module ModuleManager. Create (). UseAutofac (). UseESBIoC (). UseErpContext (). UseErpRepository ();

Business implementation

You can directly extract the corresponding warehouse from the module and then perform the corresponding curd operation.

       ModuleManager.Resolve<IRepository<User>>().Insert(new Api.User            {                Name = "two"            });

In this way, you can use efcore in the dotnet core to operate the mysql database. Note that you must addSslMode = NoneThis attribute. Otherwise, the ssl link is enabled!

Thank you for reading this article!

Back to directory

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.