ASP.NET Core 使用EF Core连接MySQL创建 Web API(基于Net.Core 3.1和Visual Studio 2019)

创建 Web 项目

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

删除无用配置及Demo

修改Properties文件夹下的launchSettings.json

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "EFGetStarted": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

删除Controller下的WeatherForecastController.cs和根目录下的WeatherForecast.cs

在这里插入图片描述

在这里插入图片描述

添加实体

新建Models文件夹

在这里插入图片描述

Models文件夹下新建类

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
    public List<Post> Posts { get; set; }
}
public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public Blog Blog { get; set; }
}
public class AuditEntry
{
    public int AuditEntryId { get; set; }
    public string Username { get; set; }
    public string Action { get; set; }
}

在这里插入图片描述

添加数据库上下文

新建Data文件夹,在其中添加数据库上下文

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
    : base(options)
    {
    }
}

在这里插入图片描述

此时程序会标红报错,这是因为缺少依赖项造成的

打开Nuget包管理器,搜索MySql.EntityFrameworkCore并安装

在这里插入图片描述

在程序中导入即可

using Microsoft.EntityFrameworkCore;

namespace EFGetStarted.Data
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
    }
}

在数据库上下文中为每个实体实在DbSet实例

using EFGetStarted.Models;
using Microsoft.EntityFrameworkCore;

namespace EFGetStarted.Data
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        public DbSet<Blog> blogs { get; set; }
        public DbSet<Post> posts { get; set; }
        public DbSet<AuditEntry> auditEntries { get; set; }
    }
}

注册数据库上下文

因为我们要使用MySQL数据库,所以需要配置MySQL的连接信息

appsettings.json配置文件中添加:

"ConnectionStrings": {
    "MysqlConnection": "Data Source=127.0.0.1;Database=ef-mysql-api;User ID=root;Password=123456789;pooling=true;port=3306;sslmode=none;CharSet=utf8;"
  },

修改后为

在这里插入图片描述

Startup.cs中注册数据库上下文

// 将服务添加到DI容器中
public void ConfigureServices(IServiceCollection services)
{
    // 读取数据库连接字符串
    var connection = Configuration.GetConnectionString("MysqlConnection");
    // 注册数据库上下文
    services.AddDbContext<ApplicationDbContext>(options => options.UseMySQL(connection));

    services.AddControllers();
}

创建第一个迁移

dotnet ef migrations add InitialCreate

在这里插入图片描述

安装Microsoft.EntityFrameworkCore.Design

在这里插入图片描述

再次运行

在这里插入图片描述

此时会多出一个Migrations文件夹

在这里插入图片描述

使用数据迁移创建在数据库中建表

dotnet ef database update

此时数据库中就建表成功了

在这里插入图片描述

修改实体模型

修改Blog的属性

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
    public List<Post> Posts { get; set; }

    public DateTime CreatedTimestamp { get; set; }
}

执行创建迁移

dotnet ef migrations add changebolg1

dotnet ef migrations add 描述性名称

更新数据库

dotnet ef database update
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值