參考文檔:https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core-example.html
開發工具:visual studio 2017 社區版
開發環境:
- win 10 企業版
- .Net Core 2.1
- MySQL 社區版 8.0.18 (安裝步驟https://www.cnblogs.com/rui-yang/p/12024437.html)
1、創建一個.Net Core 項目
2、使用NuGet添加引用【MySql.Data.EntityFrameworkCore】
在選擇版本時應該注意自己的數據庫版本,本人在此處選擇的版本是【6.10.9】,不是最新的版本,選擇好后點擊安裝即可
3、添加數據模型實體類【LibraryModel】

using System.Collections.Generic; namespace mysqlefcore { public class Book { public string ISBN { get; set; } public string Title { get; set; } public string Author { get; set; } public string Language { get; set; } public int Pages { get; set; } public virtual Publisher Publisher { get; set; } } public class Publisher { public int ID { get; set; } public string Name { get; set; } public virtual ICollection<Book> Books { get; set; } } }
4、添加數據庫連接文件【LibraryContext】
根據需要修改數據庫連接字符串

using Microsoft.EntityFrameworkCore; using MySql.Data.EntityFrameworkCore.Extensions; namespace mysqlefcore { public class LibraryContext : DbContext { public DbSet<Book> Book { get; set; } public DbSet<Publisher> Publisher { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseMySQL("server=localhost;database=library;user=root;password=Abcd@1234"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Publisher>(entity => { entity.HasKey(e => e.ID); entity.Property(e => e.Name).IsRequired(); }); modelBuilder.Entity<Book>(entity => { entity.HasKey(e => e.ISBN); entity.Property(e => e.Title).IsRequired(); entity.HasOne(d => d.Publisher) .WithMany(p => p.Books); }); } } }
5、修改【Program】類

using System; using System.Text; using Microsoft.EntityFrameworkCore; namespace mysqlefcore { class Program { static void Main(string[] args) { InsertData(); PrintData(); Console.ReadLine(); } private static void InsertData() { using (var context = new LibraryContext()) { // Creates the database if not exists context.Database.EnsureCreated(); // Adds a publisher var publisher = new Publisher { Name = "Mariner Books" }; context.Publisher.Add(publisher); // Adds some books context.Book.Add(new Book { ISBN = "978-0544003416", Title = "The Lord of the Rings", Author = "J.R.R. Tolkien", Language = "English", Pages = 1216, Publisher = publisher }); context.Book.Add(new Book { ISBN = "978-0547247763", Title = "The Sealed Letter", Author = "Emma Donoghue", Language = "English", Pages = 416, Publisher = publisher }); // Saves changes context.SaveChanges(); } } private static void PrintData() { // Gets and prints all books in database using (var context = new LibraryContext()) { var books = context.Book .Include(p => p.Publisher); foreach (var book in books) { var data = new StringBuilder(); data.AppendLine($"ISBN: {book.ISBN}"); data.AppendLine($"Title: {book.Title}"); data.AppendLine($"Publisher: {book.Publisher.Name}"); Console.WriteLine(data.ToString()); } } } } }
6、運行程序顯示結果
注:這個位置重復運行,修改了ISBN所以數據變成了4條,正常是2條