Skip to content

Commit abde9de

Browse files
authored
Use 'await using' in ASP.NET Core tutorial doc
1 parent 9db104f commit abde9de

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

docs/content/tutorials/net-core-mvc.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public class BlogPostRepository(MySqlDataSource database)
9393
{
9494
public async Task<BlogPost?> FindOneAsync(int id)
9595
{
96-
using var connection = await database.OpenConnectionAsync();
97-
using var command = connection.CreateCommand();
96+
await using var connection = await database.OpenConnectionAsync();
97+
await using var command = connection.CreateCommand();
9898
command.CommandText = @"SELECT `Id`, `Title`, `Content` FROM `BlogPost` WHERE `Id` = @id";
9999
command.Parameters.AddWithValue("@id", id);
100100
var result = await ReadAllAsync(await command.ExecuteReaderAsync());
@@ -103,24 +103,24 @@ public class BlogPostRepository(MySqlDataSource database)
103103

104104
public async Task<IReadOnlyList<BlogPost>> LatestPostsAsync()
105105
{
106-
using var connection = await database.OpenConnectionAsync();
107-
using var command = connection.CreateCommand();
106+
await using var connection = await database.OpenConnectionAsync();
107+
await using var command = connection.CreateCommand();
108108
command.CommandText = @"SELECT `Id`, `Title`, `Content` FROM `BlogPost` ORDER BY `Id` DESC LIMIT 10;";
109109
return await ReadAllAsync(await command.ExecuteReaderAsync());
110110
}
111111

112112
public async Task DeleteAllAsync()
113113
{
114-
using var connection = await database.OpenConnectionAsync();
115-
using var command = connection.CreateCommand();
114+
await using var connection = await database.OpenConnectionAsync();
115+
await using var command = connection.CreateCommand();
116116
command.CommandText = @"DELETE FROM `BlogPost`";
117117
await command.ExecuteNonQueryAsync();
118118
}
119119

120120
public async Task InsertAsync(BlogPost blogPost)
121121
{
122-
using var connection = await database.OpenConnectionAsync();
123-
using var command = connection.CreateCommand();
122+
await using var connection = await database.OpenConnectionAsync();
123+
await using var command = connection.CreateCommand();
124124
command.CommandText = @"INSERT INTO `BlogPost` (`Title`, `Content`) VALUES (@title, @content);";
125125
BindParams(command, blogPost);
126126
await command.ExecuteNonQueryAsync();
@@ -129,8 +129,8 @@ public class BlogPostRepository(MySqlDataSource database)
129129

130130
public async Task UpdateAsync(BlogPost blogPost)
131131
{
132-
using var connection = await database.OpenConnectionAsync();
133-
using var command = connection.CreateCommand();
132+
await using var connection = await database.OpenConnectionAsync();
133+
await using var command = connection.CreateCommand();
134134
command.CommandText = @"UPDATE `BlogPost` SET `Title` = @title, `Content` = @content WHERE `Id` = @id;";
135135
BindParams(command, blogPost);
136136
BindId(command, blogPost);
@@ -139,8 +139,8 @@ public class BlogPostRepository(MySqlDataSource database)
139139

140140
public async Task DeleteAsync(BlogPost blogPost)
141141
{
142-
using var connection = await database.OpenConnectionAsync();
143-
using var command = connection.CreateCommand();
142+
await using var connection = await database.OpenConnectionAsync();
143+
await using var command = connection.CreateCommand();
144144
command.CommandText = @"DELETE FROM `BlogPost` WHERE `Id` = @id;";
145145
BindId(command, blogPost);
146146
await command.ExecuteNonQueryAsync();

0 commit comments

Comments
 (0)