forked from alastairtree/LazyCache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbTimeContext.cs
35 lines (28 loc) · 1.06 KB
/
DbTimeContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace CacheDatabaseQueriesApiSample
{
public class DbTimeContext : DbContext
{
private static int databaseRequestCounter; //just for demo - don't use static fields for statistics!
public DbTimeContext(DbContextOptions<DbTimeContext> options)
: base(options)
{
}
// simulate a table in the database so we can get just one row with the current time
private DbSet<DbTimeEntity> Times { get; set; }
public static int DatabaseRequestCounter()
{
return databaseRequestCounter;
}
public DbTimeEntity GeDbTime()
{
// get the current time from SQL server right now asynchronously (simulating a slow query)
var result = Times
.FromSql("WAITFOR DELAY '00:00:00:500'; SELECT 1 as [ID], GETDATE() as [TimeNowInTheDatabase]")
.Single();
databaseRequestCounter++;
return result;
}
}
}