Skip to content

Commit 8364a44

Browse files
author
Jennifer Deigendesch
committed
Added entity framework core
1 parent 9f316e8 commit 8364a44

19 files changed

+848
-202
lines changed

ASPNETCoreExample.sln

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 14
4-
VisualStudioVersion = 14.0.25420.1
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26403.7
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F7B9D11A-69C3-4AAA-B3B4-DC19B0B53D1D}"
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{01C34A95-1B38-4C26-8EE0-CA7520486D5A}"
9-
ProjectSection(SolutionItems) = preProject
10-
global.json = global.json
11-
EndProjectSection
129
EndProject
13-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ASPNETCoreExample", "src\ASPNETCoreExample\ASPNETCoreExample.xproj", "{56CF75C7-C267-417C-BCF6-69CEBC0BF27C}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ASPNETCoreExample", "src\ASPNETCoreExample\ASPNETCoreExample.csproj", "{56CF75C7-C267-417C-BCF6-69CEBC0BF27C}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataDAL", "DataDAL\DataDAL.csproj", "{5989628F-5FC3-468B-A2A4-4C39E5EA6D2E}"
1413
EndProject
1514
Global
1615
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -22,11 +21,16 @@ Global
2221
{56CF75C7-C267-417C-BCF6-69CEBC0BF27C}.Debug|Any CPU.Build.0 = Debug|Any CPU
2322
{56CF75C7-C267-417C-BCF6-69CEBC0BF27C}.Release|Any CPU.ActiveCfg = Release|Any CPU
2423
{56CF75C7-C267-417C-BCF6-69CEBC0BF27C}.Release|Any CPU.Build.0 = Release|Any CPU
24+
{5989628F-5FC3-468B-A2A4-4C39E5EA6D2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{5989628F-5FC3-468B-A2A4-4C39E5EA6D2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
26+
{5989628F-5FC3-468B-A2A4-4C39E5EA6D2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
27+
{5989628F-5FC3-468B-A2A4-4C39E5EA6D2E}.Release|Any CPU.Build.0 = Release|Any CPU
2528
EndGlobalSection
2629
GlobalSection(SolutionProperties) = preSolution
2730
HideSolutionNode = FALSE
2831
EndGlobalSection
2932
GlobalSection(NestedProjects) = preSolution
3033
{56CF75C7-C267-417C-BCF6-69CEBC0BF27C} = {F7B9D11A-69C3-4AAA-B3B4-DC19B0B53D1D}
34+
{5989628F-5FC3-468B-A2A4-4C39E5EA6D2E} = {F7B9D11A-69C3-4AAA-B3B4-DC19B0B53D1D}
3135
EndGlobalSection
3236
EndGlobal

DataDAL/DataDAL.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace DataDAL
2+
{
3+
using global::DataDAL.Tables;
4+
5+
using Microsoft.EntityFrameworkCore;
6+
7+
public class DataDAL : DbContext
8+
{
9+
public DataDAL(DbContextOptions<DataDAL> options)
10+
: base(options)
11+
{
12+
}
13+
14+
public DbSet<User> User { get; set; }
15+
}
16+
}

DataDAL/DataDAL.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp1.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
9+
</ItemGroup>
10+
11+
</Project>

DataDAL/Tables/User.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace DataDAL.Tables
2+
{
3+
using System.ComponentModel.DataAnnotations;
4+
using System.ComponentModel.DataAnnotations.Schema;
5+
6+
[Table("User")]
7+
public class User
8+
{
9+
[Key, Required, StringLength(450)]
10+
public string Id { get; set; }
11+
12+
[StringLength(20), Required]
13+
public string Name { get; set; }
14+
}
15+
}

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
# ASP.NET Core Example
2-
<h3>This is an example of a REST API with ASP.NET Core v1.0, which is made used following tutorials:</h3>
2+
<h3>This is an example of a WebAPI with ASP.NET Core v1.0 for Visual Studio 2017, which is made used following tutorials:</h3>
33
<ul>
44
<li>Channel9: Creating REST API with ASP.NET Core (german): https://www.microsoft.com/germany/techwiese/know-how/video.aspx?id=msdn_de_67138</li>
55
<li>Managing Static Files in ASP.NET Core: https://www.youtube.com/watch?v=n3rL0ekYEOM</li>
66
<li>ASP.NET Web API Help Pages using Swagger: https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger</li>
77
<li>Middleware und Dependency Injection: https://channel9.msdn.com/Series/Einfhrung-in-ASPNET-Core/02-Middleware-und-Dependency-Injection</li>
88
<li>Logging in ASP.NET Core: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging</li>
99
</ul>
10+
<h6>Following features/frameworks are also included:</h6>
11+
<ul>
12+
<li>Entity Framework Core</li>
13+
<li>Logging via Serilog</li>
14+
<li>Attributes and Filters</li>
15+
<li>Unit-Testing</li>
16+
</ul>

global.json

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp1.0</TargetFramework>
5+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
6+
<PreserveCompilationContext>true</PreserveCompilationContext>
7+
<AssemblyName>ASPNETCoreExample</AssemblyName>
8+
<OutputType>Exe</OutputType>
9+
<PackageId>ASPNETCoreExample</PackageId>
10+
<RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
11+
<PackageTargetFallback>$(PackageTargetFallback);dotnet5.6;portable-net45+win8</PackageTargetFallback>
12+
<RootNamespace>AspNetCoreExample</RootNamespace>
13+
</PropertyGroup>
14+
15+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
16+
<DocumentationFile>bin\Debug\netcoreapp1.0\ASPNETCoreExample.xml</DocumentationFile>
17+
</PropertyGroup>
18+
19+
<ItemGroup>
20+
<None Update="wwwroot\**\*">
21+
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
22+
</None>
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.0.2" />
27+
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.0.2" />
28+
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.0.3" />
29+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.0.2" />
30+
<PackageReference Include="AutoMapper" Version="5.2.0" />
31+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="1.0.3" />
32+
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="1.0.3" />
33+
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.3" />
34+
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
35+
<PackageReference Include="Serilog.Extensions.Logging" Version="1.4.0" />
36+
<PackageReference Include="Serilog.Extensions.Logging.File" Version="1.0.1" />
37+
<PackageReference Include="Swashbuckle" Version="6.0.0-beta902" />
38+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.0.2" />
39+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.2" />
40+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.2" />
41+
</ItemGroup>
42+
43+
<ItemGroup>
44+
<ProjectReference Include="..\..\DataDAL\DataDAL.csproj" />
45+
</ItemGroup>
46+
47+
</Project>

src/ASPNETCoreExample/ASPNETCoreExample.xproj

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/ASPNETCoreExample/Controllers/FoodController.cs

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
namespace ASPNETCoreExample.Controllers {
1+
namespace ASPNETCoreExample.Controllers
2+
{
23
using System.Collections.Generic;
34
using System.Linq;
45

5-
using Dtos;
6+
using ASPNETCoreExample.Dtos;
7+
using ASPNETCoreExample.Models;
8+
using ASPNETCoreExample.Repositories;
69

710
using Microsoft.AspNetCore.JsonPatch;
811
using Microsoft.AspNetCore.Mvc;
912
using Microsoft.Extensions.Logging;
1013

11-
using Models;
12-
13-
using Repositories;
14-
15-
/// <summary>
16-
/// Controller of <see cref="IFoodRepository"/>.
17-
/// </summary>
1814
[Route("api/[controller]")]
19-
public class FoodController : Controller {
15+
public class FoodController : Controller
16+
{
2017
private readonly IFoodRepository foodRepository;
2118

2219
private readonly ILogger logger;
@@ -26,7 +23,8 @@ public class FoodController : Controller {
2623
/// </summary>
2724
/// <param name="foodRepository">Repository of <see cref="FoodItem"/>.</param>
2825
/// <param name="logger">Logger of this class.</param>
29-
public FoodController(IFoodRepository foodRepository, ILogger<FoodController> logger) {
26+
public FoodController(IFoodRepository foodRepository, ILogger<FoodController> logger)
27+
{
3028
this.foodRepository = foodRepository;
3129
this.logger = logger;
3230
}
@@ -38,7 +36,8 @@ public FoodController(IFoodRepository foodRepository, ILogger<FoodController> lo
3836
/// <response code="200">Returns all items of food.</response>
3937
[HttpGet]
4038
[ProducesResponseType(typeof(IEnumerable<FoodDto>), 200)]
41-
public IActionResult GetAllFoodItems() {
39+
public IActionResult GetAllFoodItems()
40+
{
4241
IEnumerable<FoodItem> foodItems = this.foodRepository.GetAll();
4342
IEnumerable<FoodDto> mappedItems = foodItems.Select(AutoMapper.Mapper.Map<FoodDto>);
4443

@@ -55,10 +54,12 @@ public IActionResult GetAllFoodItems() {
5554
[HttpGet("{id:int}", Name = "GetSingleFoodItem")]
5655
[ProducesResponseType(typeof(FoodDto), 404)]
5756
[ProducesResponseType(typeof(FoodDto), 200)]
58-
public IActionResult GetSingleFoodItem(int id) {
57+
public IActionResult GetSingleFoodItem(int id)
58+
{
5959
FoodItem foodItem = this.foodRepository.GetSingle(id);
6060

61-
if (foodItem == null) {
61+
if (foodItem == null)
62+
{
6263
this.logger.LogWarning("Item with {ID} not found", id);
6364
return this.NotFound();
6465
}
@@ -76,23 +77,21 @@ public IActionResult GetSingleFoodItem(int id) {
7677
[HttpPost]
7778
[ProducesResponseType(typeof(FoodDto), 400)]
7879
[ProducesResponseType(typeof(FoodDto), 201)]
79-
public IActionResult AddNewFoodItem([FromBody] FoodDto foodDto) {
80-
if (foodDto == null) {
80+
public IActionResult AddNewFoodItem([FromBody] FoodDto foodDto)
81+
{
82+
if (foodDto == null)
83+
{
8184
return this.BadRequest();
8285
}
8386

84-
if (!this.ModelState.IsValid) {
87+
if (!this.ModelState.IsValid)
88+
{
8589
return this.BadRequest(this.ModelState);
8690
}
8791

8892
FoodItem foodItem = this.foodRepository.Add(AutoMapper.Mapper.Map<FoodItem>(foodDto));
8993

90-
return this.CreatedAtRoute(
91-
"GetSingleFoodItem",
92-
new {
93-
id = foodItem.Id
94-
},
95-
AutoMapper.Mapper.Map<FoodDto>(foodItem));
94+
return this.CreatedAtRoute("GetSingleFoodItem", new { id = foodItem.Id }, AutoMapper.Mapper.Map<FoodDto>(foodItem));
9695
}
9796

9897
/// <summary>
@@ -108,17 +107,21 @@ public IActionResult AddNewFoodItem([FromBody] FoodDto foodDto) {
108107
[ProducesResponseType(typeof(FoodDto), 404)]
109108
[ProducesResponseType(typeof(FoodDto), 400)]
110109
[ProducesResponseType(typeof(FoodDto), 200)]
111-
public IActionResult UpdateFoodItem(int id, [FromBody] FoodDto foodDto) {
110+
public IActionResult UpdateFoodItem(int id, [FromBody] FoodDto foodDto)
111+
{
112112
var foodItemToCheck = this.foodRepository.GetSingle(id);
113-
if (foodItemToCheck == null) {
113+
if (foodItemToCheck == null)
114+
{
114115
return this.NotFound();
115116
}
116117

117-
if (id != foodDto.Id) {
118+
if (id != foodDto.Id)
119+
{
118120
return this.BadRequest("Ids do not match!");
119121
}
120122

121-
if (!this.ModelState.IsValid) {
123+
if (!this.ModelState.IsValid)
124+
{
122125
return this.BadRequest(this.ModelState);
123126
}
124127

@@ -139,20 +142,24 @@ public IActionResult UpdateFoodItem(int id, [FromBody] FoodDto foodDto) {
139142
[ProducesResponseType(typeof(FoodDto), 400)]
140143
[ProducesResponseType(typeof(FoodDto), 404)]
141144
[ProducesResponseType(typeof(FoodDto), 200)]
142-
public IActionResult PartialUpdate(int id, [FromBody] JsonPatchDocument<FoodDto> foodDtoPatchDoc) {
143-
if (foodDtoPatchDoc == null) {
145+
public IActionResult PartialUpdate(int id, [FromBody] JsonPatchDocument<FoodDto> foodDtoPatchDoc)
146+
{
147+
if (foodDtoPatchDoc == null)
148+
{
144149
return this.BadRequest();
145150
}
146151

147152
var foodItemExistingEntity = this.foodRepository.GetSingle(id);
148-
if (foodItemExistingEntity == null) {
153+
if (foodItemExistingEntity == null)
154+
{
149155
return this.NotFound();
150156
}
151157

152158
FoodDto foodDto = AutoMapper.Mapper.Map<FoodDto>(foodItemExistingEntity);
153159
foodDtoPatchDoc.ApplyTo(foodDto, this.ModelState);
154160

155-
if (!this.ModelState.IsValid) {
161+
if (!this.ModelState.IsValid)
162+
{
156163
return this.BadRequest(this.ModelState);
157164
}
158165

@@ -170,9 +177,11 @@ public IActionResult PartialUpdate(int id, [FromBody] JsonPatchDocument<FoodDto>
170177
[HttpDelete("{id:int}")]
171178
[ProducesResponseType(typeof(FoodDto), 404)]
172179
[ProducesResponseType(typeof(FoodDto), 204)]
173-
public IActionResult Remove(int id) {
180+
public IActionResult Remove(int id)
181+
{
174182
var foodItem = this.foodRepository.GetSingle(id);
175-
if (foodItem == null) {
183+
if (foodItem == null)
184+
{
176185
return this.NotFound();
177186
}
178187

0 commit comments

Comments
 (0)