.NetCore中EFCore for MySql整理

來源:互聯網
上載者:User

標籤:override   uri   log   資料庫   int   webapi   microsoft   報錯   ons   

一、MySql官方提供了Ef Core對MySql的支援,但現在還處於預覽版

Install-Package MySql.Data.EntityFrameworkCore -PreInstall-Package MySql.Data.EntityFrameworkCore.Design -Pre

當前程式包依賴Microsoft.EntityFrameworkCore,目前沒找到API文檔啥的,MySql.Data.EntityFrameworkCore.Design的用法也沒 找到。

Nuget包地址 :https://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/7.0.7-m61

https://www.nuget.org/packages/MySql.Data.EntityFrameworkCore.Design/6.10.1-beta

二、簡單使用樣本

1.手寫資料庫上下文

/// <summary>/// 資料庫上下文/// </summary>public class Md5Context : DbContext{    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)    {        optionsBuilder.UseMySQL("server=127.0.0.1;port=3306;database=md5data;uid=sa;pwd=123;");    }    public virtual DbSet<Md5Data> Md5Data { get; set; }}

2.手寫實體資料庫映射,Model

[Table("Md5Data")] //當前預覽版中Table指定名稱還不支援public class Md5Data{    [Key]    public int DataID { get; set; }    public string PlainText { get; set; }    public string UpperCase32 { get; set; }    public string UpperCase16 { get; set; }    public int DencryptCount { get; set; }    public int EncryptCount { get; set; }    public DateTime AddTime { get; set; }}

3.調試運行

    Md5Context _Context = new Md5Context();    List<Md5Data> list = _Context.Md5Data.ToList();    return list.Select(q => q.PlainText).ToList();

三、說明

1.

這裡需要注意了:

public DbSet<Blog> blog { get; set; }

如果mysql裡的表名blog是小寫,這裡就要定義成跟表名完全一樣,否則會報錯

2.Table() 表名的注釋,目前還不支援。

 

更多:

Asp.Net Core WebAPI入門整理(三)跨域處理

Asp.Net Core WebAPI入門整理(二)簡單樣本

Asp.Net Core中Json序列化處理整理

.NetCore中EFCore for MySql整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.