.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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/291921.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2021 开源社年度报告:开心开源

# 引言 #2020年的开源社年度报告仿佛还在昨天&#xff0c;一转眼&#xff0c;2021年都已经过完了。在去年的年度报告上&#xff0c;我们说2020是动荡不安的一年。结果2021年&#xff0c;简直可以说是动荡加剧&#xff0c;令人应接不暇的一年。迫于疫情的影响&#xff0c;我们一…

luajit日记-FFI库

2019独角兽企业重金招聘Python工程师标准>>> LuaJIT FFI LibraryThe FFI library allows calling external C functions and using C data structures from pure Lua code. The FFI library largely obviates the need to write tedious manual Lua/C bindings in …

C#10 和 .NET6 代码跨平台开发

零、前言有数千页长的编程书籍&#xff0c;旨在成为 C# 语言、.NET 库、网站、服务、桌面和移动应用等应用模型的综合参考。这本书不一样。它简洁明了&#xff0c;旨在成为一本轻快有趣的书&#xff0c;每一个主题都有实用的实践演练。总体叙述的广度是以某种深度为代价的&…

linux之tar命令使用总结

1、使用原因 刚才在linux平台需要安装Clion的时候,下载得到CLion-2016.3.2.tar.gz 这个gz的压缩文件,所以需要解压到当前文件夹 2、简单解压到当前文件 解压当前文件夹命令 tar -zxvf CLion-2016.3.2.tar.gz 效果如下 3、tar命令介绍 -c: 建立压缩档案 -x:解压 -t:…

体验 正式发布 的OSM v1.0.0 版本

2021年10月份发布了OSM 1.0 RC[1]&#xff0c;在过去的几个月里&#xff0c;OSM 的贡献者一直在努力为 v1.0.0 版本的发布做准备。2022年2月1日&#xff0c;OSM 团队正式发布 1.0.0 版本[2]。OSM 从最初的发布到现在已经走了很长的路&#xff0c;团队继续专注于社区需要的关键和…

数据流图的画法

数据流图的画法 数据流图也称为数据流程图date flow diagram , DFD&#xff0c;是一种便于用户理解和分析系统数据流程的图形工具&#xff0c;他摆脱了系统和详细内容&#xff0c;精确的在逻辑上描写叙述系统的功能、输入、输出和数据存储等&#xff0c;是系统逻辑模型的重要组…

MFC继承表

转载于:https://www.cnblogs.com/Lthis/p/4264967.html

linux之fdisk查看分区和mkfs.ext3删除分区和mount挂载和e2label添加卷标使用总结

一、使用fdisk、mkfs.ext3、和mount、e2lable的原因 有个分区挂载不上,然后需要格式化分区,还需要添加卷标 二、fdisk、mkfs.ext3、mount、e2lable命令介绍 1、fdisk命令介绍 1)、了解分区 分区是将一个硬盘驱动器分成若干个逻辑驱动器,分区是把硬盘连续的区块当做一个…

linux c之strncpy函数和strncmp函数最简单使用总结

1.原型声明&#xff1a; char * strncpy(char *dest,const char *src, size_t n); strncmp() 用来比较两个字符串的前n个字符&#xff0c;区分大小写&#xff0c;其原型为&#xff1a; int strncmp ( const char * str1, const char * str2, size_t n ); 若str1与str2的前n…

阻止你变现的,从来都不是开源许可证

文 | lola_chen出品 | OSC开源社区&#xff08;ID&#xff1a;oschina2013&#xff09;之前&#xff0c;《GPL 转闭源&#xff1f;法院判决&#xff1a;一日 GPL 终身 GPL》一文提出一个冷门却又重要的知识点&#xff1a;GPL 许可证之下的开源项目&#xff0c;可以分叉出来闭源…

03-递归

数据结构和算法 基于《算法图解》—Aditya Bhargava和《数据结构》—严蔚敏 第3章 递归 3.1 递归 假设在一堆嵌套的盒子里找钥匙&#xff0c;对比循环和递归。 使用循环解决&#xff1a; #使用while循环&#xff1a;只要盒子堆不是空&#xff0c;就从中取出一个盒子&#x…

linux c之提示format‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat

1、问题 有个long int data;我输出的时候printf("data is %d", data);出现下面警告 自己竟然不知道 长整型怎么打印出来&#xff0c;日了狗。 2、解决办法 md&#xff0c;m为指定的输出字段的宽度。如果数据的位数小于m&#xff0c;则左端补以空格&#xff0c;若大…

Asp.Net Core部署:早知道,还是docker!以及一点碎碎念

前言AspNetCore技术栈在我们团队里的使用也有一段时间了&#xff0c;之前的部署方式一直是本地编译之后上传可执行文件到服务器&#xff0c;使用supervisor来管理进程这种很原始的方式。参考之前的文章&#xff1a;Asp.Net Core学习笔记&#xff1a;&#xff08;五&#xff09;…

04-快速排序

数据结构和算法 基于《算法图解》—Aditya Bhargava 和《数据结构》—严蔚敏 第4章 快速排序 4.1 分而治之 divide and conquer , 简称D&C&#xff1a;一种著名的递归式问题解决方法。 例子1&#xff1a; 假设你是农场主&#xff0c;有一小块土地。要求将这块地均匀地分…

android studio no marked region found along edge Found along top edge

由于种种原因&#xff0c;导致9图已经不是9图格式了&#xff0c;但是在Eclipse里面不会报错&#xff0c;在android studio 里面会报错 Error:9-patch image D:\download\avatar-android-master\avatar-android-master\sample\src\main\res\drawable-hdpi\abc_list_divider_holo…

Linux下安装compsoer ,并使用composer安装laravel

为什么80%的码农都做不了架构师&#xff1f;>>> 1、composer安装 https://getcomposer.org/download/打开composer官网。根据提示linux下可以使用php安装。 php -r "copy(https://getcomposer.org/installer, composer-setup.php);" php -r "if (ha…

AspNetCore在docker部署时遇到一个小坑

哦吼之前刚说了尝试了使用docker来部署AspNetCore应用&#xff08;Asp.Net Core部署&#xff1a;早知道&#xff0c;还是docker!以及一点碎碎念&#xff09;&#xff0c;结果这才刚上班就遇到问题了 …我这项目用的数据库是Oracle&#xff0c;之前直接运行没啥问题&#xff0c;…

05-散列表

数据结构和算法 基于《算法图解》—Aditya Bhargava 和《数据结构》—严蔚敏 第5章 散列表 假设你在一家杂货店上班。有顾客来买东西时&#xff0c;你得到一个本子中查找价格。如果本子的内容不是按字母顺序来排序的&#xff0c;你可能为查找苹果价格而浏览每一页&#xff0c…

疯狂ios讲义疯狂连载之实现游戏逻辑(2)

13.6.5 定义获取通道的工具方法这里所谓的通道指的是一个方块上、下、左、右4个方向上的空白方块图13.10显示了一个方块四周的通道。图13.10 方块四周的通道下面是获取某个坐标点四周通道的4个方法。程序清单codes/13/Link/Link/sources/board/FKGameService.m13.6.6 没有转折点…

linux之进程间通信--使用信号

一、什么是信号用过Windows的我们都知道&#xff0c;当我们无法正常结束一个程序时&#xff0c;可以用任务管理器强制结束这个进程&#xff0c;但这其实是怎么实现的呢&#xff1f;同样的功能在Linux上是通过生成信号和捕获信号来实现的&#xff0c;运行中的进程捕获到这个信号…