Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
yoyomooc
asp.net-core-mvc-2019
Commits
bbd0402e
Commit
bbd0402e
authored
Sep 28, 2020
by
梁桐铭
🏅
Browse files
完成efcore的存储过程操作。
parent
2d522321
Changes
20
Hide whitespace changes
Inline
Side-by-side
RazorPages/StudentManagement.RazorPage.Services/AppDbContext.cs
0 → 100644
View file @
bbd0402e
using
Microsoft.EntityFrameworkCore
;
using
StudentManagement.RazorPage.Models
;
namespace
StudentManagement.RazorPage.Services
{
public
class
AppDbContext
:
DbContext
{
public
AppDbContext
(
DbContextOptions
<
AppDbContext
>
options
)
:
base
(
options
)
{
}
public
DbSet
<
Student
>
Students
{
get
;
set
;
}
}
}
\ No newline at end of file
RazorPages/StudentManagement.RazorPage.Services/IStudentRepository.cs
View file @
bbd0402e
...
...
@@ -16,5 +16,10 @@ namespace StudentManagement.RazorPage.Services
Student
Delete
(
int
id
);
IEnumerable
<
ClassHeadCount
>
StudentCountByClassNameEnum
(
ClassNameEnum
?
className
);
IEnumerable
<
Student
>
Search
(
string
searchTerm
);
}
}
\ No newline at end of file
RazorPages/StudentManagement.RazorPage.Services/Migrations/20200928032301_InitialCreate.Designer.cs
0 → 100644
View file @
bbd0402e
// <auto-generated />
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.EntityFrameworkCore.Infrastructure
;
using
Microsoft.EntityFrameworkCore.Metadata
;
using
Microsoft.EntityFrameworkCore.Migrations
;
using
Microsoft.EntityFrameworkCore.Storage.ValueConversion
;
using
StudentManagement.RazorPage.Services
;
namespace
StudentManagement.RazorPage.Services.Migrations
{
[
DbContext
(
typeof
(
AppDbContext
))]
[
Migration
(
"20200928032301_InitialCreate"
)]
partial
class
InitialCreate
{
protected
override
void
BuildTargetModel
(
ModelBuilder
modelBuilder
)
{
#pragma warning disable 612, 618
modelBuilder
.
HasAnnotation
(
"ProductVersion"
,
"3.1.8"
)
.
HasAnnotation
(
"Relational:MaxIdentifierLength"
,
128
)
.
HasAnnotation
(
"SqlServer:ValueGenerationStrategy"
,
SqlServerValueGenerationStrategy
.
IdentityColumn
);
modelBuilder
.
Entity
(
"StudentManagement.RazorPage.Models.Student"
,
b
=>
{
b
.
Property
<
int
>(
"Id"
)
.
ValueGeneratedOnAdd
()
.
HasColumnType
(
"int"
)
.
HasAnnotation
(
"SqlServer:ValueGenerationStrategy"
,
SqlServerValueGenerationStrategy
.
IdentityColumn
);
b
.
Property
<
int
>(
"ClassName"
)
.
HasColumnType
(
"int"
);
b
.
Property
<
string
>(
"Email"
)
.
IsRequired
()
.
HasColumnType
(
"nvarchar(max)"
);
b
.
Property
<
string
>(
"Name"
)
.
IsRequired
()
.
HasColumnType
(
"nvarchar(50)"
)
.
HasMaxLength
(
50
);
b
.
Property
<
string
>(
"PhotoPath"
)
.
HasColumnType
(
"nvarchar(max)"
);
b
.
HasKey
(
"Id"
);
b
.
ToTable
(
"Students"
);
});
#pragma warning restore 612, 618
}
}
}
RazorPages/StudentManagement.RazorPage.Services/Migrations/20200928032301_InitialCreate.cs
0 → 100644
View file @
bbd0402e
using
Microsoft.EntityFrameworkCore.Migrations
;
namespace
StudentManagement.RazorPage.Services.Migrations
{
public
partial
class
InitialCreate
:
Migration
{
protected
override
void
Up
(
MigrationBuilder
migrationBuilder
)
{
migrationBuilder
.
CreateTable
(
name
:
"Students"
,
columns
:
table
=>
new
{
Id
=
table
.
Column
<
int
>(
nullable
:
false
)
.
Annotation
(
"SqlServer:Identity"
,
"1, 1"
),
Name
=
table
.
Column
<
string
>(
maxLength
:
50
,
nullable
:
false
),
ClassName
=
table
.
Column
<
int
>(
nullable
:
false
),
Email
=
table
.
Column
<
string
>(
nullable
:
false
),
PhotoPath
=
table
.
Column
<
string
>(
nullable
:
true
)
},
constraints
:
table
=>
{
table
.
PrimaryKey
(
"PK_Students"
,
x
=>
x
.
Id
);
});
}
protected
override
void
Down
(
MigrationBuilder
migrationBuilder
)
{
migrationBuilder
.
DropTable
(
name
:
"Students"
);
}
}
}
RazorPages/StudentManagement.RazorPage.Services/Migrations/20200928034640_spGetStudentById.Designer.cs
0 → 100644
View file @
bbd0402e
// <auto-generated />
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.EntityFrameworkCore.Infrastructure
;
using
Microsoft.EntityFrameworkCore.Metadata
;
using
Microsoft.EntityFrameworkCore.Migrations
;
using
Microsoft.EntityFrameworkCore.Storage.ValueConversion
;
using
StudentManagement.RazorPage.Services
;
namespace
StudentManagement.RazorPage.Services.Migrations
{
[
DbContext
(
typeof
(
AppDbContext
))]
[
Migration
(
"20200928034640_spGetStudentById"
)]
partial
class
spGetStudentById
{
protected
override
void
BuildTargetModel
(
ModelBuilder
modelBuilder
)
{
#pragma warning disable 612, 618
modelBuilder
.
HasAnnotation
(
"ProductVersion"
,
"3.1.8"
)
.
HasAnnotation
(
"Relational:MaxIdentifierLength"
,
128
)
.
HasAnnotation
(
"SqlServer:ValueGenerationStrategy"
,
SqlServerValueGenerationStrategy
.
IdentityColumn
);
modelBuilder
.
Entity
(
"StudentManagement.RazorPage.Models.Student"
,
b
=>
{
b
.
Property
<
int
>(
"Id"
)
.
ValueGeneratedOnAdd
()
.
HasColumnType
(
"int"
)
.
HasAnnotation
(
"SqlServer:ValueGenerationStrategy"
,
SqlServerValueGenerationStrategy
.
IdentityColumn
);
b
.
Property
<
int
>(
"ClassName"
)
.
HasColumnType
(
"int"
);
b
.
Property
<
string
>(
"Email"
)
.
IsRequired
()
.
HasColumnType
(
"nvarchar(max)"
);
b
.
Property
<
string
>(
"Name"
)
.
IsRequired
()
.
HasColumnType
(
"nvarchar(50)"
)
.
HasMaxLength
(
50
);
b
.
Property
<
string
>(
"PhotoPath"
)
.
HasColumnType
(
"nvarchar(max)"
);
b
.
HasKey
(
"Id"
);
b
.
ToTable
(
"Students"
);
});
#pragma warning restore 612, 618
}
}
}
RazorPages/StudentManagement.RazorPage.Services/Migrations/20200928034640_spGetStudentById.cs
0 → 100644
View file @
bbd0402e
using
Microsoft.EntityFrameworkCore.Migrations
;
namespace
StudentManagement.RazorPage.Services.Migrations
{
public
partial
class
spGetStudentById
:
Migration
{
protected
override
void
Up
(
MigrationBuilder
migrationBuilder
)
{
string
procedure
=
@"Create Procedure spGetStudentById
@Id int
as
Begin
Select * from Students
Where Id = @Id
End"
;
migrationBuilder
.
Sql
(
procedure
);
}
protected
override
void
Down
(
MigrationBuilder
migrationBuilder
)
{
string
procedure
=
@"Drop procedure spGetStudentById"
;
migrationBuilder
.
Sql
(
procedure
);
}
}
}
\ No newline at end of file
RazorPages/StudentManagement.RazorPage.Services/Migrations/AppDbContextModelSnapshot.cs
0 → 100644
View file @
bbd0402e
// <auto-generated />
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.EntityFrameworkCore.Infrastructure
;
using
Microsoft.EntityFrameworkCore.Metadata
;
using
Microsoft.EntityFrameworkCore.Storage.ValueConversion
;
using
StudentManagement.RazorPage.Services
;
namespace
StudentManagement.RazorPage.Services.Migrations
{
[
DbContext
(
typeof
(
AppDbContext
))]
partial
class
AppDbContextModelSnapshot
:
ModelSnapshot
{
protected
override
void
BuildModel
(
ModelBuilder
modelBuilder
)
{
#pragma warning disable 612, 618
modelBuilder
.
HasAnnotation
(
"ProductVersion"
,
"3.1.8"
)
.
HasAnnotation
(
"Relational:MaxIdentifierLength"
,
128
)
.
HasAnnotation
(
"SqlServer:ValueGenerationStrategy"
,
SqlServerValueGenerationStrategy
.
IdentityColumn
);
modelBuilder
.
Entity
(
"StudentManagement.RazorPage.Models.Student"
,
b
=>
{
b
.
Property
<
int
>(
"Id"
)
.
ValueGeneratedOnAdd
()
.
HasColumnType
(
"int"
)
.
HasAnnotation
(
"SqlServer:ValueGenerationStrategy"
,
SqlServerValueGenerationStrategy
.
IdentityColumn
);
b
.
Property
<
int
>(
"ClassName"
)
.
HasColumnType
(
"int"
);
b
.
Property
<
string
>(
"Email"
)
.
IsRequired
()
.
HasColumnType
(
"nvarchar(max)"
);
b
.
Property
<
string
>(
"Name"
)
.
IsRequired
()
.
HasColumnType
(
"nvarchar(50)"
)
.
HasMaxLength
(
50
);
b
.
Property
<
string
>(
"PhotoPath"
)
.
HasColumnType
(
"nvarchar(max)"
);
b
.
HasKey
(
"Id"
);
b
.
ToTable
(
"Students"
);
});
#pragma warning restore 612, 618
}
}
}
RazorPages/StudentManagement.RazorPage.Services/MockStudentRepository.cs
View file @
bbd0402e
...
...
@@ -78,5 +78,16 @@ namespace StudentManagement.RazorPage.Services
}
return
student
;
}
public
IEnumerable
<
Student
>
Search
(
string
searchTerm
=
null
)
{
if
(
string
.
IsNullOrEmpty
(
searchTerm
))
{
return
_studentList
;
}
return
_studentList
.
Where
(
e
=>
e
.
Name
.
Contains
(
searchTerm
)
||
e
.
Email
.
Contains
(
searchTerm
)).
ToList
();
}
}
}
\ No newline at end of file
RazorPages/StudentManagement.RazorPage.Services/SQLStudentRepository.cs
0 → 100644
View file @
bbd0402e
using
Microsoft.Data.SqlClient
;
using
Microsoft.EntityFrameworkCore
;
using
StudentManagement.RazorPage.Models
;
using
System.Collections.Generic
;
using
System.Linq
;
namespace
StudentManagement.RazorPage.Services
{
public
class
SQLStudentRepository
:
IStudentRepository
{
private
readonly
AppDbContext
context
;
public
SQLStudentRepository
(
AppDbContext
context
)
{
this
.
context
=
context
;
}
public
Student
Add
(
Student
newStudent
)
{
context
.
Students
.
Add
(
newStudent
);
context
.
SaveChanges
();
return
newStudent
;
}
public
Student
Delete
(
int
id
)
{
Student
student
=
context
.
Students
.
Find
(
id
);
if
(
student
!=
null
)
{
context
.
Students
.
Remove
(
student
);
context
.
SaveChanges
();
}
return
student
;
}
public
IEnumerable
<
Student
>
GetAllStudents
()
{
return
context
.
Students
.
FromSqlRaw
<
Student
>(
"SELECT * FROM Students"
)
.
ToList
();
}
public
Student
GetStudent
(
int
id
)
{
SqlParameter
parameter
=
new
SqlParameter
(
"@Id"
,
id
);
return
context
.
Students
.
FromSqlRaw
<
Student
>(
"spGetStudentById {0}"
,
parameter
)
.
ToList
()
.
FirstOrDefault
();
}
public
IEnumerable
<
Student
>
Search
(
string
searchTerm
)
{
if
(
string
.
IsNullOrEmpty
(
searchTerm
))
{
return
context
.
Students
;
}
return
context
.
Students
.
Where
(
e
=>
e
.
Name
.
Contains
(
searchTerm
)
||
e
.
Email
.
Contains
(
searchTerm
));
}
public
IEnumerable
<
ClassHeadCount
>
StudentCountByClassNameEnum
(
ClassNameEnum
?
className
)
{
IEnumerable
<
Student
>
query
=
context
.
Students
;
if
(
className
.
HasValue
)
{
query
=
query
.
Where
(
e
=>
e
.
ClassName
==
className
.
Value
);
}
return
query
.
GroupBy
(
e
=>
e
.
ClassName
)
.
Select
(
g
=>
new
ClassHeadCount
()
{
ClassName
=
g
.
Key
.
Value
,
Count
=
g
.
Count
()
}).
ToList
();
}
public
Student
Update
(
Student
updatedStudent
)
{
var
student
=
context
.
Students
.
Attach
(
updatedStudent
);
student
.
State
=
Microsoft
.
EntityFrameworkCore
.
EntityState
.
Modified
;
context
.
SaveChanges
();
return
updatedStudent
;
}
}
}
\ No newline at end of file
RazorPages/StudentManagement.RazorPage.Services/StudentManagement.RazorPage.Services.csproj
View file @
bbd0402e
...
...
@@ -4,6 +4,12 @@
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.8" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StudentManagement.RazorPage.Models\StudentManagement.RazorPage.Models.csproj" />
</ItemGroup>
...
...
RazorPages/StudentManagement.RazorPage/Pages/Index.cshtml
View file @
bbd0402e
...
...
@@ -11,5 +11,9 @@
@await Component.InvokeAsync("HeadCount")
<vc:head-count
class-name=
"null"
></vc:head-count>
<p>
Learn about
<a
href=
"https://docs.microsoft.com/aspnet/core"
>
building Web apps with ASP.NET Core
</a>
.
</p>
</div>
RazorPages/StudentManagement.RazorPage/Pages/Privacy.cshtml
View file @
bbd0402e
...
...
@@ -4,6 +4,10 @@
ViewData["Title"] = "Privacy Policy";
}
<h1>
@ViewData["Title"]
</h1>
@await Component.InvokeAsync("HeadCount", null)
@*
<vc:head-count
class-name=
"null"
></vc:head-count>
*@
@*
<vc:head-count
class-name=
"ClassNameEnum.FirstGrade"
></vc:head-count>
*@
<p>
Use this page to detail your site's privacy policy.
</p>
RazorPages/StudentManagement.RazorPage/Pages/Students/Details.cshtml
View file @
bbd0402e
...
...
@@ -6,61 +6,62 @@
var photoPath = "~/Images/" + (Model.Student.PhotoPath ?? "noimage.png");
}
<div
class=
"row justify-content-center m-3"
>
<div
class=
"row justify-content-center m-3"
>
@await Component.InvokeAsync("HeadCount", new { className = Model.Student.ClassName })
@await Component.InvokeAsync("HeadCount", new { className = Model.Student.ClassName })
<vc:head-count
class-name=
"Model.Student.ClassName"
></vc:head-count>
<div
class=
"col-sm-8"
>
<div
class=
"col-sm-8"
>
@if (TempData["message"] != null)
{
<div
class=
"col-sm-8"
>
<div
class=
"alert alert-primary"
>
@TempData["message"]
@if (TempData["message"] != null)
{
<div
class=
"col-sm-8"
>
<div
class=
"alert alert-primary"
>
@TempData["message"]
</div>
</div>
</div>
}
}
@if (Model.Message != null)
{
<div
class=
"col-sm-8"
>
<div
class=
"alert alert-primary"
>
@Model.Message
@if (Model.Message != null)
{
<div
class=
"col-sm-8"
>
<div
class=
"alert alert-primary"
>
@Model.Message
</div>
</div>
</div>
}
}
@if (TempData.Peek("message") != null)
{
<div
class=
"col-sm-8"
>
<div
class=
"alert alert-primary"
>
@TempData.Peek("message")
@if (TempData.Peek("message") != null)
{
<div
class=
"col-sm-8"
>
<div
class=
"alert alert-primary"
>
@TempData.Peek("message")
</div>
</div>
</div>
}
}
<div
class=
"card"
>
<div
class=
"card-header"
>
<h1>
@Model.Student.Name
</h1>
</div>
<div
class=
"card"
>
<div
class=
"card-header"
>
<h1>
@Model.Student.Name
</h1>
</div>
<div
class=
"card-body text-center"
>
<img
class=
"card-img-top"
src=
"@photoPath"
asp-append-version=
"true"
style=
"width:50%"
/>
<div
class=
"card-body text-center"
>
<img
class=
"card-img-top"
src=
"@photoPath"
asp-append-version=
"true"
style=
"width:50%"
/>
<h4>
Student ID : @Model.Student.Id
</h4>
<h4>
邮箱地址 : @Model.Student.Email
</h4>
<h4>
班级 : @Model.Student.ClassName
</h4>
<h4>
Student ID : @Model.Student.Id
</h4>
<h4>
邮箱地址 : @Model.Student.Email
</h4>
<h4>
班级 : @Model.Student.ClassName
</h4>
</div>
<div
class=
"card-footer text-center"
>
<a
asp-page=
"index"
class=
"btn btn-primary"
>
返回
</a>
@*
<a
href=
"#"
class=
"btn btn-primary"
>
编辑
</a>
</div>
<div
class=
"card-footer text-center"
>
<a
asp-page=
"index"
class=
"btn btn-primary"
>
返回
</a>
@*
<a
href=
"#"
class=
"btn btn-primary"
>
编辑
</a>
<a
href=
"#"
class=
"btn btn-danger"
>
删除
</a>
*@
</div>
</div>
</div>
</div>
</div>
\ No newline at end of file
</div>
\ No newline at end of file
RazorPages/StudentManagement.RazorPage/Pages/Students/Index.cshtml
View file @
bbd0402e
...
...
@@ -14,12 +14,19 @@
}
</style>
<h1>
学生
</h1>
@await Component.InvokeAsync("HeadCount", new
{
className = ClassNameEnum.FirstGrade
})
<h1>
学生列表
</h1>
@await Component.InvokeAsync("HeadCount")
<form
method=
"get"
>
<div
class=
"input-group"
>
<input
class=
"form-control"
asp-for=
"SearchTerm"
>
<div
class=
"input-group-append"
>
<button
class=
"btn btn-primary"
type=
"submit"
>
搜索
</button>
</div>
</div>
</form>
<div
class=
"card-deck"
>
@foreach (var student in Model.Students)
{
...
...
RazorPages/StudentManagement.RazorPage/Pages/Students/Index.cshtml.cs
View file @
bbd0402e
using
Microsoft.AspNetCore.Mvc
;
using
Microsoft.AspNetCore.Mvc.RazorPages
;
using
StudentManagement.RazorPage.Models
;
using
StudentManagement.RazorPage.Services
;
...
...
@@ -8,13 +9,14 @@ namespace StudentManagement.RazorPage.Pages.Students
public
class
IndexModel
:
PageModel
{
private
readonly
IStudentRepository
studentRepository
;
/// <summary>
/// //这个公共属性保存学生列表 显示模板(Index.html)可以访问此属性
/// </summary>
public
IEnumerable
<
Student
>
Students
{
get
;
set
;
}
[
BindProperty
(
SupportsGet
=
true
)]
public
string
SearchTerm
{
get
;
set
;
}
/// <summary>
/// 注册IStudentRepository服务。通过这项服务知道如何查询学生列表
...
...
@@ -26,11 +28,11 @@ namespace StudentManagement.RazorPage.Pages.Students
}
/// <summary>
/// 此方法处理发送GET请求 到路由 /Students/Index
/// 此方法处理发送GET请求 到路由 /Students/Index
/// </summary>
public
void
OnGet
()
{
Students
=
studentRepository
.
GetAllStudents
(
);
Students
=
studentRepository
.
Search
(
SearchTerm
);
}
}
}
\ No newline at end of file
RazorPages/StudentManagement.RazorPage/Pages/_ViewImports.cshtml
View file @
bbd0402e
...
...
@@ -3,3 +3,6 @@
@namespace StudentManagement.RazorPage.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, StudentManagement.RazorPage
@*//StudentManagement.RazorPage,是包含要注册的视图组件的程序集(DLL) *(星号),指定所有的视图组件。*@
\ No newline at end of file
RazorPages/StudentManagement.RazorPage/Properties/launchSettings.json
View file @
bbd0402e
{
{
"iisSettings"
:
{
"windowsAuthentication"
:
false
,
"anonymousAuthentication"
:
true
,
"iisExpress"
:
{
"applicationUrl"
:
"http://localhost:22
2
1"
,
"applicationUrl"
:
"http://localhost:22
4
1"
,
"sslPort"
:
0
}
},
...
...
RazorPages/StudentManagement.RazorPage/Startup.cs
View file @
bbd0402e
using
Microsoft.AspNetCore.Builder
;