.NET6之MiniAPI(十二):引入EntityFramewor

2023-11-05 19:48

本文主要是介绍.NET6之MiniAPI(十二):引入EntityFramewor,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

说明:本篇重点说明MiniAPI引入EntityFramework,EF的使用不是本篇的重点

本篇是在MiniAPI中使用EntityFramework,所以先奉上创建数据的脚本,数据库是SQL Server,可能版本不同,会有一些问题,可以自行调整,这个脚本是基于SQL Server2019的。

USE [Exam]
GO
/****** Object:  Table [dbo].[Answers]    Script Date: 2022/1/22 10:01:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Answers]([ID] [int] IDENTITY(1,1) NOT NULL,[Sequre] [nvarchar](8) NOT NULL,[Answer] [nvarchar](200) NOT NULL,[IsTrue] [bit] NOT NULL,[QuestionID] [int] NOT NULL,CONSTRAINT [PK_Answers] PRIMARY KEY CLUSTERED 
([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[ExamPaperQuestions]    Script Date: 2022/1/22 10:01:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ExamPaperQuestions]([ExamPaperID] [int] NOT NULL,[QuestionID] [int] NOT NULL,CONSTRAINT [PK_ExamPaperQuestions_1] PRIMARY KEY CLUSTERED 
([ExamPaperID] ASC,[QuestionID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[ExamPapers]    Script Date: 2022/1/22 10:01:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ExamPapers]([ID] [int] IDENTITY(1,1) NOT NULL,[Title] [nvarchar](200) NOT NULL,[Memo] [nvarchar](1000) NULL,[CreateTime] [datetime2](7) NOT NULL,CONSTRAINT [PK_ExamPapers] PRIMARY KEY CLUSTERED 
([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Questions]    Script Date: 2022/1/22 10:01:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Questions]([ID] [int] IDENTITY(1,1) NOT NULL,[Question] [nvarchar](300) NOT NULL,[Score] [float] NOT NULL,[QuestionTypeID] [int] NOT NULL,[SujectTypeID] [int] NOT NULL,CONSTRAINT [PK_Questions] PRIMARY KEY CLUSTERED 
([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[QuestionTypes]    Script Date: 2022/1/22 10:01:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[QuestionTypes]([ID] [int] IDENTITY(1,1) NOT NULL,[TypeName] [nvarchar](50) NOT NULL,CONSTRAINT [PK_QuestionTypes] PRIMARY KEY CLUSTERED 
([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[SubjectTypes]    Script Date: 2022/1/22 10:01:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SubjectTypes]([ID] [int] IDENTITY(1,1) NOT NULL,[TypeName] [nvarchar](50) NOT NULL,CONSTRAINT [PK_SubjectTypes] PRIMARY KEY CLUSTERED 
([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[UserExamAnswers]    Script Date: 2022/1/22 10:01:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UserExamAnswers]([ID] [int] IDENTITY(1,1) NOT NULL,[UserExamID] [int] NOT NULL,[AnswerID] [int] NOT NULL,[CreateTime] [datetime] NOT NULL,CONSTRAINT [PK_UserExamAnswers] PRIMARY KEY CLUSTERED 
([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[UserExams]    Script Date: 2022/1/22 10:01:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UserExams]([ID] [int] IDENTITY(1,1) NOT NULL,[UserID] [int] NOT NULL,[ExamPapgerID] [int] NOT NULL,[BeginTime] [datetime] NOT NULL,[EndTime] [datetime] NOT NULL,CONSTRAINT [PK_UserExams] PRIMARY KEY CLUSTERED 
([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[Users]    Script Date: 2022/1/22 10:01:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Users]([ID] [int] IDENTITY(1,1) NOT NULL,[UserName] [nvarchar](50) NOT NULL,[Password] [nvarchar](50) NOT NULL,[Salt] [nvarchar](50) NOT NULL,[Name] [nvarchar](50) NOT NULL,[Tel] [varchar](11) NULL,CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED 
([ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Answers] ON INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (1, N'A', N'循环', 0, 1)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (2, N'B', N'指针 ', 1, 1)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (3, N'C', N'判断 ', 0, 1)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (4, N'D', N'数组', 0, 1)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (5, N'A', N'只读', 1, 2)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (6, N'B', N'只写', 0, 2)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (7, N'C', N'可读写', 0, 2)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (8, N'D', N'不可读不可写', 0, 2)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (9, N'A', N'break', 1, 3)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (10, N'B', N'goto', 0, 3)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (11, N'C', N'return ', 0, 3)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (12, N'D', N'continue', 0, 3)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (13, N'A', N'class', 0, 4)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (14, N'B', N'using', 1, 4)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (16, N'D', N'this', 0, 4)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (17, N'A', N'click', 0, 5)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (18, N'B', N'catch', 0, 5)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (19, N'C', N'try', 1, 5)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (20, N'D', N'show', 0, 5)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (21, N'A', N'该复选框被选中', 1, 6)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (22, N'B', N'该复选框不被选中', 0, 6)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (23, N'C', N'不显示该复选框的文本信息', 0, 6)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (24, N'D', N'显示该复选框的文本信息', 0, 6)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (25, N'A', N'System', 0, 7)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (26, N'B', N'System.Data', 0, 7)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (27, N'C', N'System.Data.OleDb', 0, 7)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (28, N'D', N'System.Data.SqlClient', 1, 7)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (29, N'A', N'FileStream类 ', 1, 8)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (30, N'B', N'StreamReade', 0, 8)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (31, N'C', N'BinaryWriter类', 0, 8)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (32, N'D', N'BinaryReader', 0, 8)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (33, N'A', N'输入、编译、运行', 1, 9)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (34, N'B', N'编译、连接、运行', 0, 9)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (35, N'C', N'输入、运行、编辑', 0, 9)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (36, N'D', N'编辑、编译、连接', 0, 9)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (37, N'A', N'int转换成short', 0, 10)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (38, N'B', N'short转换成long', 0, 10)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (39, N'C', N'char转换成int', 0, 10)
INSERT [dbo].[Answers] ([ID], [Sequre], [Answer], [IsTrue], [QuestionID]) VALUES (40, N'D', N'bytes转换成float', 1, 10)
SET IDENTITY_INSERT [dbo].[Answers] OFF
GO
INSERT [dbo].[ExamPaperQuestions] ([ExamPaperID], [QuestionID]) VALUES (1, 1)
INSERT [dbo].[ExamPaperQuestions] ([ExamPaperID], [QuestionID]) VALUES (1, 2)
INSERT [dbo].[ExamPaperQuestions] ([ExamPaperID], [QuestionID]) VALUES (1, 3)
INSERT [dbo].[ExamPaperQuestions] ([ExamPaperID], [QuestionID]) VALUES (1, 4)
INSERT [dbo].[ExamPaperQuestions] ([ExamPaperID], [QuestionID]) VALUES (1, 5)
INSERT [dbo].[ExamPaperQuestions] ([ExamPaperID], [QuestionID]) VALUES (1, 6)
INSERT [dbo].[ExamPaperQuestions] ([ExamPaperID], [QuestionID]) VALUES (1, 7)
INSERT [dbo].[ExamPaperQuestions] ([ExamPaperID], [QuestionID]) VALUES (1, 8)
INSERT [dbo].[ExamPaperQuestions] ([ExamPaperID], [QuestionID]) VALUES (1, 9)
INSERT [dbo].[ExamPaperQuestions] ([ExamPaperID], [QuestionID]) VALUES (1, 10)
GO
SET IDENTITY_INSERT [dbo].[ExamPapers] ON INSERT [dbo].[ExamPapers] ([ID], [Title], [Memo], [CreateTime]) VALUES (1, N'C#初级试题一(2021)', N'2021年10月生成', CAST(N'2021-12-13T22:26:44.0129362' AS DateTime2))
INSERT [dbo].[ExamPapers] ([ID], [Title], [Memo], [CreateTime]) VALUES (2, N'C#初级试题二(2021)', N'2021年11月生成', CAST(N'2021-12-13T22:26:44.0129382' AS DateTime2))
INSERT [dbo].[ExamPapers] ([ID], [Title], [Memo], [CreateTime]) VALUES (3, N'C#初级试题三(2021)', N'2021年12月生成', CAST(N'2021-12-13T22:26:44.0129383' AS DateTime2))
SET IDENTITY_INSERT [dbo].[ExamPapers] OFF
GO
SET IDENTITY_INSERT [dbo].[Questions] ON INSERT [dbo].[Questions] ([ID], [Question], [Score], [QuestionTypeID], [SujectTypeID]) VALUES (1, N'C#语言取消了(  )语法。', 10, 1, 1)
INSERT [dbo].[Questions] ([ID], [Question], [Score], [QuestionTypeID], [SujectTypeID]) VALUES (2, N'以下类MyClass的属性count属于( )属性。
class MyClass
{int i;int count { get { return i; } }
}', 10, 1, 1)
INSERT [dbo].[Questions] ([ID], [Question], [Score], [QuestionTypeID], [SujectTypeID]) VALUES (3, N'( )语句只能在循环语句的循环体语句序列中使用。', 10, 1, 1)
INSERT [dbo].[Questions] ([ID], [Question], [Score], [QuestionTypeID], [SujectTypeID]) VALUES (4, N'在C#应用程序中,一般在程序的开头使用关键字( )来引入命名空间。', 10, 1, 1)
INSERT [dbo].[Questions] ([ID], [Question], [Score], [QuestionTypeID], [SujectTypeID]) VALUES (5, N'异常处理使用时,一般将可能出现异常的语句放在( )代码块中。', 10, 1, 1)
INSERT [dbo].[Questions] ([ID], [Question], [Score], [QuestionTypeID], [SujectTypeID]) VALUES (6, N'WinForms程序中,如果复选框控件的 Checked属性值设置为 True,表示( )。', 10, 1, 1)
INSERT [dbo].[Questions] ([ID], [Question], [Score], [QuestionTypeID], [SujectTypeID]) VALUES (7, N'在ADO.NET中,SqlConnection 类所在的命名空间是( )。', 10, 1, 1)
INSERT [dbo].[Questions] ([ID], [Question], [Score], [QuestionTypeID], [SujectTypeID]) VALUES (8, N'下面哪个类是用来以字节格式读写文件( )。', 10, 1, 1)
INSERT [dbo].[Questions] ([ID], [Question], [Score], [QuestionTypeID], [SujectTypeID]) VALUES (9, N'C#程序从上机到得到结果的几个操作步骤依次是( )。', 10, 1, 1)
INSERT [dbo].[Questions] ([ID], [Question], [Score], [QuestionTypeID], [SujectTypeID]) VALUES (10, N'下面的转换中不是隐式转换的是( )。', 10, 1, 1)
SET IDENTITY_INSERT [dbo].[Questions] OFF
GO
SET IDENTITY_INSERT [dbo].[QuestionTypes] ON INSERT [dbo].[QuestionTypes] ([ID], [TypeName]) VALUES (1, N'单项选择题')
INSERT [dbo].[QuestionTypes] ([ID], [TypeName]) VALUES (2, N'多项选择题')
INSERT [dbo].[QuestionTypes] ([ID], [TypeName]) VALUES (3, N'判断题')
SET IDENTITY_INSERT [dbo].[QuestionTypes] OFF
GO
SET IDENTITY_INSERT [dbo].[SubjectTypes] ON INSERT [dbo].[SubjectTypes] ([ID], [TypeName]) VALUES (1, N'C Sharp(C#)')
INSERT [dbo].[SubjectTypes] ([ID], [TypeName]) VALUES (2, N'SQL Server')
SET IDENTITY_INSERT [dbo].[SubjectTypes] OFF
GO
SET IDENTITY_INSERT [dbo].[UserExams] ON INSERT [dbo].[UserExams] ([ID], [UserID], [ExamPapgerID], [BeginTime], [EndTime]) VALUES (1, 1, 1, CAST(N'2021-12-01T00:00:00.000' AS DateTime), CAST(N'2022-12-01T00:00:00.000' AS DateTime))
SET IDENTITY_INSERT [dbo].[UserExams] OFF
GO
SET IDENTITY_INSERT [dbo].[Users] ON INSERT [dbo].[Users] ([ID], [UserName], [Password], [Salt], [Name], [Tel]) VALUES (1, N'zhangsan', N'@f232fd(feef', N'sfw32==', N'张三', N'13456879562')
INSERT [dbo].[Users] ([ID], [UserName], [Password], [Salt], [Name], [Tel]) VALUES (2, N'lisi', N'@22ewfd(feef', N'42syt==', N'李四', N'13456879562')SET IDENTITY_INSERT [dbo].[Users] OFF
GO
ALTER TABLE [dbo].[ExamPapers] ADD  DEFAULT (getdate()) FOR [CreateTime]
GO
ALTER TABLE [dbo].[UserExamAnswers] ADD  DEFAULT (getdate()) FOR [CreateTime]
GO
ALTER TABLE [dbo].[Answers]  WITH CHECK ADD  CONSTRAINT [FK_Answers_Questions] FOREIGN KEY([QuestionID])
REFERENCES [dbo].[Questions] ([ID])
GO
ALTER TABLE [dbo].[Answers] CHECK CONSTRAINT [FK_Answers_Questions]
GO
ALTER TABLE [dbo].[ExamPaperQuestions]  WITH CHECK ADD  CONSTRAINT [FK_ExamPaperQuestions_ExamPapers] FOREIGN KEY([ExamPaperID])
REFERENCES [dbo].[ExamPapers] ([ID])
GO
ALTER TABLE [dbo].[ExamPaperQuestions] CHECK CONSTRAINT [FK_ExamPaperQuestions_ExamPapers]
GO
ALTER TABLE [dbo].[ExamPaperQuestions]  WITH CHECK ADD  CONSTRAINT [FK_ExamPaperQuestions_Questions] FOREIGN KEY([QuestionID])
REFERENCES [dbo].[Questions] ([ID])
GO
ALTER TABLE [dbo].[ExamPaperQuestions] CHECK CONSTRAINT [FK_ExamPaperQuestions_Questions]
GO
ALTER TABLE [dbo].[Questions]  WITH CHECK ADD  CONSTRAINT [FK_Questions_QuestionTypes] FOREIGN KEY([QuestionTypeID])
REFERENCES [dbo].[QuestionTypes] ([ID])
GO
ALTER TABLE [dbo].[Questions] CHECK CONSTRAINT [FK_Questions_QuestionTypes]
GO
ALTER TABLE [dbo].[Questions]  WITH CHECK ADD  CONSTRAINT [FK_Questions_SubjectTypes] FOREIGN KEY([SujectTypeID])
REFERENCES [dbo].[SubjectTypes] ([ID])
GO
ALTER TABLE [dbo].[Questions] CHECK CONSTRAINT [FK_Questions_SubjectTypes]
GO
ALTER TABLE [dbo].[UserExamAnswers]  WITH CHECK ADD  CONSTRAINT [FK_UserExamAnswers_Answers] FOREIGN KEY([AnswerID])
REFERENCES [dbo].[Answers] ([ID])
GO
ALTER TABLE [dbo].[UserExamAnswers] CHECK CONSTRAINT [FK_UserExamAnswers_Answers]
GO
ALTER TABLE [dbo].[UserExamAnswers]  WITH CHECK ADD  CONSTRAINT [FK_UserExamAnswers_UserExams] FOREIGN KEY([UserExamID])
REFERENCES [dbo].[UserExams] ([ID])
GO
ALTER TABLE [dbo].[UserExamAnswers] CHECK CONSTRAINT [FK_UserExamAnswers_UserExams]
GO
ALTER TABLE [dbo].[UserExams]  WITH CHECK ADD  CONSTRAINT [FK_UserExams_ExamPapers] FOREIGN KEY([ExamPapgerID])
REFERENCES [dbo].[ExamPapers] ([ID])
GO
ALTER TABLE [dbo].[UserExams] CHECK CONSTRAINT [FK_UserExams_ExamPapers]
GO
ALTER TABLE [dbo].[UserExams]  WITH CHECK ADD  CONSTRAINT [FK_UserExams_Users] FOREIGN KEY([UserID])
REFERENCES [dbo].[Users] ([ID])
GO
ALTER TABLE [dbo].[UserExams] CHECK CONSTRAINT [FK_UserExams_Users]
GO

有了数据库后,接下来开始在Mini API项目中引入EntityFramework,首选安装dotnet-ef的tool:

dotnet tool install --global dotnet-ef

再在项目中引入nuget包  Microsoft.EntityFrameworkCore.Design和Microsoft.EntityFrameworkCore.SqlServer

这时就可以从数据生成EF的实体类了:

dotnet ef dbcontext scaffold "server=.;database=Exam;uid=sa;pwd=sa;" Microsoft.EntityFrameworkCore.SqlServer -o Models

dd43a69bdac954eb63a2626edefca0fa.png

对生成的EF实体类进行整理,因为在导出实体类时,我们给了他一个连接字符串,所以在生成的实体类中是有这个连字符串的,打开ExamContext.cs,删除选中OnConfiguring这个方法。

fbaf609a53a80bffe8626cdcb56066d3.png

在appsetings.json中配置连接字符串

"ConnectionStrings": {"ExamDatabase": "server=.;database=Exam;uid=sa;pwd=sa;"}

引入EF用AddDbContext,参数Options决定有什么类型的数据库,和数据库连接字符串。然后在MapGet中从服务容器中取到ExamContext使用就可以。

using MiniDemo04.Models;
using Microsoft.EntityFrameworkCore;
using System.Text.Json.Serialization;var builder = WebApplication.CreateBuilder(args);builder.Services.AddDbContext<ExamContext>(options =>options.UseSqlServer(builder.Configuration.GetConnectionString("ExamDatabase")));var app = builder.Build();app.MapGet("/exampaper/{id}", async (ExamContext exam, int id) =>{return Results.Json(await exam.ExamPapers.Include(paper => paper.Questions).ThenInclude(question => question.Answers).Include(paper => paper.Questions).ThenInclude(question => question.QuestionType)   .Select(paper => new { paper.Id, paper.Title, paper.CreateTime, Scores = paper.Questions.Sum(s => s.Score), Count = paper.Questions.Count, Questions = paper.Questions.Select(question => new { Question = $"{question.Id}、{question.Question1}({question.Score}分  {question.QuestionType.TypeName})", Answers = question.Answers.Select(answer => new { answer.Sequre, Answer = answer.Answer1 }) }) }).FirstOrDefaultAsync(s => s.Id == id), new System.Text.Json.JsonSerializerOptions { ReferenceHandler = ReferenceHandler.IgnoreCycles });});app.Run();

运行结果:

67cd7f131b94dd839f558c0257056bec.png

这篇关于.NET6之MiniAPI(十二):引入EntityFramewor的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/352092

相关文章

SpringBoot项目引入token设置方式

《SpringBoot项目引入token设置方式》本文详细介绍了JWT(JSONWebToken)的基本概念、结构、应用场景以及工作原理,通过动手实践,展示了如何在SpringBoot项目中实现JWT... 目录一. 先了解熟悉JWT(jsON Web Token)1. JSON Web Token是什么鬼

SW - 引入第三方dwg图纸后,修改坐标原点

文章目录 SW - 引入第三方dwg图纸后,修改坐标原点概述笔记设置图纸新原点END SW - 引入第三方dwg图纸后,修改坐标原点 概述 在solidworks中引入第三方的dwg格式图纸后,坐标原点大概率都不合适。 全图自动缩放后,引入的图纸离默认的原点位置差很多。 需要自己重新设置原点位置,才能自动缩放后,在工作区中间显示引入的图纸。 笔记 将dwg图纸拖到SW中

react笔记 8-17 属性绑定 class绑定 引入图片 循环遍历

1、绑定属性 constructor(){super()this.state={name:"张三",title:'我是一个title'}}render() {return (<div><div>aaaaaaa{this.state.name}<div title={this.state.title}>我是一个title</div></div></div>)} 绑定属性直接使用花括号{}   注

YOLOv8改进实战 | 注意力篇 | 引入CVPR2024 PKINet 上下文锚点注意力CAAttention

YOLOv8专栏导航:点击此处跳转 前言 YOLOv8 是由 YOLOv5 的发布者 Ultralytics 发布的最新版本的 YOLO。它可用于对象检测、分割、分类任务以及大型数据集的学习,并且可以在包括 CPU 和 GPU 在内的各种硬件上执行。 YOLOv8 是一种尖端的、最先进的 (SOTA) 模型,它建立在以前成功的 YOLO 版本的基础上,并引入了新的功能和改进,以

RISC-V (十二)系统调用

系统模式:用户态和内核态         当前的代码都是实现在machine模式下。 系统模式的切换         epc寄存器的值存放的是ecall指本身的地址 。 用ecall指令 系统调用的执行流程         mret这条指令会利用status的mpp值恢复到之前的特权级别。  蓝色的线表示涉及到权限切换。  系统调用的传参

fetch-event-source 如何通过script全局引入

fetchEventSource源码中导出了两种类型的包cjs和esm。但是有个需求如何在原生是js中通过script标签引呢?需要加上type=module。今天介绍另一种方法 下载源码文件: https://github.com/Azure/fetch-event-source.git 安装: npm install --save-dev webpack webpack-cli ts

YOLOv8改进 | Conv篇 | YOLOv8引入DWR

1. DWR介绍 1.1  摘要:当前的许多工作直接采用多速率深度扩张卷积从一个输入特征图中同时捕获多尺度上下文信息,从而提高实时语义分割的特征提取效率。 然而,这种设计可能会因为结构和超参数的不合理而导致多尺度上下文信息的访问困难。 为了降低多尺度上下文信息的绘制难度,我们提出了一种高效的多尺度特征提取方法,将原始的单步方法分解为区域残差-语义残差两个步骤。 在该方法中,多速率深度扩张卷积

机器学习模型中的因果关系:引入单调约束

单调约束是使机器学习模型可行的关键,但它们仍未被广泛使用欢迎来到雲闪世界。 碳ausality 正在迅速成为每个数据科学家工具包中必不可少的组成部分。 这是有充分理由的。 事实上,因果模型在商业中具有很高的价值,因为它们为“假设”情景提供了更可靠的估计,特别是在用于做出影响业务结果的决策时。 在本文中,我将展示如何通过简单的更改(实际上添加一行代码)将传统的 ML 模型(如随机森林、L

Kafka【十二】消费者拉取主题分区的分配策略

【1】消费者组、leader和follower 消费者想要拉取主题分区的数据,首先必须要加入到一个组中。 但是一个组中有多个消费者的话,那么每一个消费者该如何消费呢,是不是像图中一样的消费策略呢?如果是的话,那假设消费者组中只有2个消费者或有4个消费者,和分区的数量不匹配,怎么办? 所以这里,我们需要学习Kafka中基本的消费者组中的消费者和分区之间的分配规则: 同一个消费者组的消费者都订

C++11中新引入的enum类型

一、引言         在C++11中,引入了一个新的枚举类型——enum class(或称为作用域枚举类型),它相较于传统的enum类型有几个关键的不同点。enum class提供了更强的类型检查和封装,减少了命名冲突的可能性,并允许更加清晰地表达枚举的意图。 二、传统枚举(enum)的缺点         在C++11之前,枚举(enum)类型是隐式转换为整型的,这可能会导致一些意料之