参考文档: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条