@@ -46,11 +46,9 @@ public async Task<ChatUser> CreateUserAsync(string name, CancellationToken cance
46
46
name = await NormalizeNameAsync ( name , cancellationToken ) . ConfigureAwait ( false ) ;
47
47
await using var dbContext = CreateDbContext ( ) ;
48
48
49
- var userEntry = dbContext . ChatUsers . Add ( new ChatUser ( ) {
50
- Name = name
51
- } ) ;
49
+ var user = new ChatUser ( ) { Name = name } ;
50
+ await dbContext . ChatUsers . AddAsync ( user , cancellationToken ) . ConfigureAwait ( false ) ;
52
51
await dbContext . SaveChangesAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
53
- var user = userEntry . Entity ;
54
52
55
53
// Invalidation
56
54
Computed . Invalidate ( ( ) => GetUserAsync ( user . Id , CancellationToken . None ) ) ;
@@ -63,9 +61,9 @@ public async Task<ChatUser> SetUserNameAsync(long id, string name, CancellationT
63
61
name = await NormalizeNameAsync ( name , cancellationToken ) . ConfigureAwait ( false ) ;
64
62
await using var dbContext = CreateDbContext ( ) ;
65
63
66
- var user = await GetUserAsync ( id , cancellationToken ) . ConfigureAwait ( false ) ;
64
+ var user = await dbContext . ChatUsers . AsQueryable ( )
65
+ . SingleAsync ( u => u . Id == id , cancellationToken ) . ConfigureAwait ( false ) ;
67
66
user . Name = name ;
68
- dbContext . ChatUsers . Update ( user ) ;
69
67
await dbContext . SaveChangesAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
70
68
71
69
// Invalidation
@@ -79,13 +77,13 @@ public async Task<ChatMessage> AddMessageAsync(long userId, string text, Cancell
79
77
await using var dbContext = CreateDbContext ( ) ;
80
78
81
79
await GetUserAsync ( userId , cancellationToken ) . ConfigureAwait ( false ) ; // Check to ensure the user exists
82
- var messageEntry = dbContext . ChatMessages . Add ( new ChatMessage ( ) {
80
+ var message = new ChatMessage ( ) {
83
81
CreatedAt = DateTime . UtcNow ,
84
82
UserId = userId ,
85
83
Text = text ,
86
- } ) ;
84
+ } ;
85
+ await dbContext . ChatMessages . AddAsync ( message , cancellationToken ) . ConfigureAwait ( false ) ;
87
86
await dbContext . SaveChangesAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
88
- var message = messageEntry . Entity ;
89
87
90
88
// Invalidation
91
89
Computed . Invalidate ( EveryChatTail ) ;
0 commit comments