ef core操作mysql当前官方提供的最新版本是:8.0.1

需要安装的包分别是:

MySql.Data

MySql.Data.EntityFrameworkCore

MySql.Data.EntityFrameworkCore.Design

 

其他的安装包不用安装,如果安装了其他版本的依赖包则会报错;

数据迁移需要导入Microsoft.EntityFrameworkCore.Design,版本是2.0.0;

尝试了最新版本的2.1.1但是不行,在数据操作时候报错。

接下来就可以连接mysql了

public class Blog
    {
        public int Id { get; set; }
        public  string Title { get; set; }
        public  DateTime CreateTime { get; set; }
    }
public class AppDbContext:DbContext
    {
        public AppDbContext()  
        {
           
        }
        public  DbSet<Blog> Blogs { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
      //SslModel=None 这和ssl协议有关系。如果不指定会报错 optionsBuilder.UseMySQL(
"server=localhost;user=root;database=test;port=3306;password=****;SslMode=None"); } }

 1  class Program
 2     {
 3         
 4         static void Main(string[] args)
 5         {
 6 
 7             using (var db = new AppDbContext())
 8             {
 9                 db.Database.EnsureCreated();
10                 db.Blogs.Add(new Blog { Title = "http://blogs.msdn.com/adonet",CreateTime = DateTime.Now});
11                 var count = db.SaveChanges();
12                 Console.WriteLine("{0} records saved to database", count);
13 
14                 Console.WriteLine();
15                 Console.WriteLine("All blogs in database:");
16                 foreach (var blog in db.Blogs)
17                 {
18                     Console.WriteLine(" - {0}", blog.Title);
19                 }
20             }
21 
22             Console.ReadLine();
23         }
24     }
View Code

相关文章:

  • 2022-12-23
  • 2020-10-15
  • 2021-04-05
  • 2022-12-23
  • 2022-12-23
  • 2020-06-01
  • 2022-12-23
  • 2021-10-18
猜你喜欢
  • 2022-12-23
  • 2018-11-30
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
  • 2022-12-23
  • 2021-07-06
相关资源
相似解决方案