Doraemon

小叮当    2012 - 2023
Doraemon

Choose mode

  • dark
  • auto
  • light
首页
Category
  • 前端开发
  • 后端
  • 数据库
  • 运维
Tag
TimeLine
关于
  • 关于我
Contact
  • GitHub
author-avatar

小叮当

39

Article

25

Tag

首页
Category
  • 前端开发
  • 后端
  • 数据库
  • 运维
Tag
TimeLine
关于
  • 关于我
Contact
  • GitHub

使用VS Code开发.NET Core 0005 - 使用EF Core DBFirst

小叮当    2012 - 2023

使用VS Code开发.NET Core 0005 - 使用EF Core DBFirst


小叮当 2020-11-14 .Net Core后端Dot-net-with-vs-code

# 一、安装工具,dotnet-ef

安裝

dotnet tool install --global dotnet-ef
1

指定版本安裝

dotnet tool install --global dotnet-ef --version 3.0.0
1

卸载 dotnet-ef

dotnet tool uninstall -g dotnet-ef
1

# 二、安装数据库依赖

1、必须添加依赖Microsoft.EntityFrameworkCore.Design

dotnet add package Microsoft.EntityFrameworkCore.Design
1

2、添加数据库驱动

不建议使用 MySql.Data.EntityFrameworkCore,因为此驱动只支持 MySQL,并且通过 EFCore DBFirst 生成的 Model 数据类型不全,如时间、Guid 都无法识别。

MySql.Data.EntityFrameworkCore
1

or

建议使用此驱动,不仅支持 MySQL,还支持 MariaDB。而且使用 EFCore DBFirst 可以识别所有的字段类型。

Pomelo.EntityFrameworkCore.MySql
1

微软官方说明

# 三、EF Core 从数据库生成代码

官方链接

安装了驱动MySql.Data.EntityFrameworkCore使用此命令

dotnet ef dbcontext scaffold "server=localhost;port=3306;user=root;password=123456;database=DBName" MySql.Data.EntityFrameworkCore -o DBName -f
1

or

安装了驱动Pomelo.EntityFrameworkCore.MySql使用此命令

dotnet ef dbcontext scaffold "server=localhost;port=3306;user=root;password=123456;database=DBName" Pomelo.EntityFrameworkCore.Mysql -f -o Models
1

创建 MySQL 数据库命令

create database name character set utf8mb4 collate utf8mb4_general_ci;
1

# 四、将生成的代码复制到对应的代码层

1、将实体类复制到 Do 层 2、将 Context 复制到仓储(Repository)层,并且做如下修改 首先,删除如下代码:

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
                optionsBuilder.UseMySql("server=localhost;port=3306;user=root;password=123456;database=secmon", x => x.ServerVersion("8.0.17-mysql"));
            }
        }
1
2
3
4
5
6
7
8

其次,在 Extensions 层创建 ServiceExtensions: DbSetup

    public static class DbSetup
    {
        public static void AddDbSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));

            // services.AddScoped<SecMonContext>(); //如果数据库链接字符串写在SecMonContext,则使用此注入方式

            services.AddDbContext<SecMonContext>(options =>
            {
                options.UseMySql("server=localhost;port=3306;user=root;password=123456;database=secmon",
                                 x => x.ServerVersion("8.0.17-mysql"));
            });
        }
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 五、创建 HttpContextSetup

在 Extensions 层创建 ServiceExtensions: HttpContextSetup

    public static class HttpContextSetup
    {
        public static void AddHttpContextSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));

            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        }
    }
1
2
3
4
5
6
7
8
9

注意

在 Startup 中使用AddDbSetup 和 AddHttpContextAccessor

  services.AddDbSetup();
  services.AddHttpContextAccessor();
1
2

注意

  • 一、安装工具,dotnet-ef
  • 二、安装数据库依赖
  • 三、EF Core 从数据库生成代码
  • 四、将生成的代码复制到对应的代码层
  • 五、创建 HttpContextSetup