当前位置: 首页 > news >正文

efcore反向共工程,单元测试

1.安装efcore需要的nuget

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.24" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.24" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.24" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.23"><PrivateAssets>all</PrivateAssets><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets></PackageReference>

2.安装efcore cli工具

dotnet tool install --global dotnet-ef

3.在终端中执行下面的命令
在这里插入图片描述

 dotnet ef dbcontext scaffold "server=.;uid=sa;pwd=peng@123;database=ide" Microsoft.EntityFrameworkCore.SqlServer --output-dir Models -c EFcoreDbContext

执行完成后生成你数据库中对应的实体代码和数据上下文
在这里插入图片描述
4.每次的查询结果输出sql(在上下文中添加logTo方法)

  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){//每次运行时生成sql语句optionsBuilder.LogTo(Console.WriteLine, LogLevel.Information);if (!optionsBuilder.IsConfigured){
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.optionsBuilder.UseSqlServer("server=.;uid=sa;pwd=peng@123;database=ide");}}

5.安装nunit单元测试Nuget

  <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /><PackageReference Include="NUnit" Version="3.13.2" /><PackageReference Include="NUnit3TestAdapter" Version="4.3.0" />

6.创建测试类

using EfcoreTest.Models;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EfcoreTest
{public class Test1{[Test]public async Task serach(){using (EFcoreDbContext efcore = new EFcoreDbContext()){var list = await efcore.AspNetRoles.ToListAsync();Console.WriteLine(list.Count);}}}
}

7.点击方法右击运行测试

在这里插入图片描述
8.查看输出结果
在这里插入图片描述
也可以在测试资源管理器中查看
在这里插入图片描述
补充:运行时提示有多个入口点,需要指定入口文件
项目右击-属性-应用程序-常规-启动对象(选择自己项目启动的文件入口)
在这里插入图片描述

http://www.lryc.cn/news/230371.html

相关文章:

  • 利用IP风险画像强化金融行业网络安全防御
  • 1334. 阈值距离内邻居最少的城市
  • Live800:客服行业的发展历程及未来前景
  • exsi的安装和配置
  • 基于springboot实现校园医疗保险管理系统【项目源码】
  • Python 如何实现组合(Composite)设计模式?什么是组合设计模式?
  • 编辑器vim和编译器gcc/g++
  • linux 系统下文本编辑常用的命令
  • 3D Gaussian Splatting文件的压缩【3D高斯泼溅】
  • Spring Boot 整合xxl-job实现分布式定时任务
  • 16.最接近的三数之和
  • php 插入排序算法实现
  • import gradio时出现SyntaxError: future feature annotations is not defined解决方案
  • 视频封装格式
  • vue+iView实现下载zip文件导出多个excel表格
  • Rust编程中的共享状态并发执行
  • python语法之数据类型
  • Skybox天空盒子的更换教程_unity基础开发教程
  • Android模拟器的linux内核源码的下载
  • Vue中methods实现原理
  • 维基百科是非营利性机构 词条内容具有中立性、准确性、可靠性
  • C/C++轻量级并发TCP服务器框架Zinx-框架开发002: 定义通道抽象类
  • bin、hex、ELF文件格式上的区别
  • 《QT从基础到进阶·二十六》绘制多个图形项(QGraphicsRectItem,QGraphicsLineItem,QGraphicsPolygonItem)
  • 【分布式】CAP理论详解
  • AI歌姬,C位出道,基于PaddleHub/Diffsinger实现音频歌声合成操作(Python3.10)
  • ZooKeeper基本知识
  • leetcode:138. 随机链表的复制
  • SpringBoot 全局异常之参数校验(1)
  • QT windows与linux之间sokcet通信中文乱码问题解决方法