导入
Pomelo.EntityFrameworkCore.MySql
public class BloggingContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseMySql("server=localhost;userid=root;pwd=xxx;port=3306;database=world;"); } public class Blog { public int BlogId { get; set; } public string Url { get; set; } public List<Post> Posts { get; } = new List<Post>(); } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } }
包控制台输入
Install-Package Microsoft.EntityFrameworkCore.Tools //引入包 Add-Migration InitialCreate //生成文件 Update-Database //更新到数据库