第一步:选择ASP.NET Core Web创建C#解决方案
![](https://img2020.cnblogs.com/blog/427549/202008/427549-20200827173648591-20768383.png)
第二步:重命名项目名称,创建项目
![](https://img2020.cnblogs.com/blog/427549/202008/427549-20200827173810184-1341657798.png)
第三步:选择模板创建Web程序
![](https://img2020.cnblogs.com/blog/427549/202008/427549-20200827173948731-2110825922.png)
第四步:安装三个NuGet包
Microsoft.EntityFrameworkCore.Design、Microsoft.EntityFrameworkCore.SqlServer、Microsoft.EntityFrameworkCore.Tools
第五步:项目Models文件夹添加类文件
![](https://img2020.cnblogs.com/blog/427549/202008/427549-20200827175012418-574257041.png)
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public virtual List<Post> Posts { get; set; }
}
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; }
}
第六步:在appsettings.json中添加链接数据库字符串
![](https://img2020.cnblogs.com/blog/427549/202008/427549-20200827175247508-1684931638.png)
第七步:在ConfigureServices方法中添加上下文依赖注入
![](https://img2020.cnblogs.com/blog/427549/202008/427549-20200827175727183-843154070.png)
第八步:打开程序包管理器控制台
![](https://img2020.cnblogs.com/blog/427549/202008/427549-20200827175932899-112238811.png)
第九步:先输入 Add-Migration FirstMigration
![](https://img2020.cnblogs.com/blog/427549/202008/427549-20200827180014717-1605202977.png)
第十步:在输入Update-Database。迁移成功后,会创建数据库,以及会在项目中生成一个Migrations文件夹,里面时迁移记录。
![](https://img2020.cnblogs.com/blog/427549/202008/427549-20200827180116137-809456614.png)
第十一步:自动生成的数据库截图
![](https://img2020.cnblogs.com/blog/427549/202008/427549-20200827180422321-1432069969.png)