ASP.NET Core 1.0 使用 MySQL for EF Core 1.0 (.NET Core 1.0)

经过多天的项目设计实践,本来都几乎放弃使用 MySQL 去使用 MSSQL ,但几经周折还是在找到了在 .NET Core 中使用 MySQL 数据库的方法(官方的进度不敢恭维,现使用的是第三方库),在此分享一下。

添加引用 Pomelo.EntityFrameworkCore.MySql ,使用 1.0.0 版本即可。

2016/7/27 nele :这不是国外的,是国内的。https://github.com/PomeloFoundation,柚子基金会是微软mvp 郑逸笙创建的用于宣传.net core技术,其中已经开源了很多项目或者框架。

2016/8/8 该库已经可以在 NuGet 中下载了。

最后以往 Core First 该怎么用现在就怎么用。

以下附上我的示例:

 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)
        {
        }

       //省略实体定义

        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         }

 

转载于:https://www.cnblogs.com/VicBilibily/p/5679353.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值