ASP. NET Core 1.0 uses MySQL for EF Core 1.0 (. NET Core 1.0) and coremysql

Source: Internet
Author: User

ASP. NET Core 1.0 uses MySQL for EF Core 1.0 (. NET Core 1.0) and coremysql

After several days of project design practices, I almost gave up using MySQL to use MSSQL, but I found it abroad after several twists and turns.. NET Core uses the MySQL database method (the official progress is not flattering, but a third-party library is currently used.

First, configure the NuGet package source because the package has not yet been released to the NuGet library.

Package Source: https://www.myget.org/F/pomelo/api/v2/

Then add and reference Pomelo. EntityFrameworkCore. MySql, and use version 1.0.0. (According to the message, this package can be found in NuGet source in early August)

In the end, how to use Core First is now used.

My example is attached as follows:

1 public void ConfigureServices(IServiceCollection services)
 2         {
 3 
 4             // Add framework services.
 5             services.AddMvc();
 6 
 7             services.AddDbContext<DBContext>(options => options.UseMySql(Option.EntityContextSql));
 8 
 9             services.Configure<Microsoft.AspNetCore.Server.Kestrel.KestrelServerOptions>(option =>
10  { 11                 option.UseHttps(Path.Combine(new DirectoryInfo(Directory.GetCurrentDirectory()).FullName, "cret.pfx"), "pw");
12             });
13         }


using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DB
{
     public class DBContext: DbContext
     {
         public DBContext (DbContextOptions <DBContext> options): base (options)
         {
         }

        // Omit entity definition

         private static readonly IServiceProvider _serviceProvider = new ServiceCollection (). AddEntityFrameworkMySql (). BuildServiceProvider ();
         protected override void OnConfiguring (DbContextOptionsBuilder optionsBuilder)
         {
             base.OnConfiguring (optionsBuilder);
             optionsBuilder.UseInternalServiceProvider (_serviceProvider) .UseMySql (Option.EntityContextSql);
         }

         protected override void OnModelCreating (ModelBuilder modelBuilder)
         {
             base.OnModelCreating (modelBuilder);
         }

     }
}

1         public static string EntityContextSql
2         {
3             get
4             {
5                 return string.Format("Data Source={0};port={1};user id={2};password={3};database={4};Charset=utf8;", EntityConfig.dataSource, EntityConfig.port, EntityConfig.id, EntityConfig.pw, EntityConfig.db);
6             }
7         } 
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.