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 }