-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyDbContext.cs
More file actions
30 lines (26 loc) · 894 Bytes
/
MyDbContext.cs
File metadata and controls
30 lines (26 loc) · 894 Bytes
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
using System.Data.Common;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace Blog
{
public class MyDbContext : DbContext
{
private static SqliteConnection _connection;
public DbSet<BlogPost> BlogPosts { get; set; }
public DbSet<BlogComment> BlogComments { get; set; }
public MyDbContext(ILoggerFactory loggerFactory) : base(new DbContextOptionsBuilder<MyDbContext>()
.UseSqlite(CreateInMemoryDatabase())
.UseLoggerFactory(loggerFactory)
.Options)
{
}
private static DbConnection CreateInMemoryDatabase()
{
_connection = new SqliteConnection("Filename=:memory:");
_connection.Open();
return _connection;
}
public override void Dispose() => _connection.Dispose();
}
}