tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
SqliteDatabaseContext.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3
4using Microsoft.EntityFrameworkCore;
5using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
6
8
10{
15 {
19 readonly bool designTime;
20
26 DbContextOptions<SqliteDatabaseContext> dbContextOptions)
27 : this(dbContextOptions, false)
28 {
29 }
30
36 internal SqliteDatabaseContext(
37 DbContextOptions<SqliteDatabaseContext> dbContextOptions,
38 bool designTime)
39 : base(dbContextOptions)
40 {
41 this.designTime = designTime;
42 }
43
49 public static void ConfigureWith(
50 DbContextOptionsBuilder options,
51 DatabaseConfiguration databaseConfiguration)
52 {
53 ArgumentNullException.ThrowIfNull(options);
54 ArgumentNullException.ThrowIfNull(databaseConfiguration);
55
56 if (databaseConfiguration.DatabaseType != DatabaseType.Sqlite)
57 throw new InvalidOperationException($"Invalid DatabaseType for {nameof(SqliteDatabaseContext)}!");
58
59 options.UseSqlite(databaseConfiguration.ConnectionString, sqliteOptions => sqliteOptions.UseQuerySplittingBehavior(QuerySplittingBehavior.SingleQuery));
60 }
61
63 protected override void OnModelCreating(ModelBuilder modelBuilder)
64 {
65 base.OnModelCreating(modelBuilder);
66
67 // Shamelessly stolen from https://blog.dangl.me/archive/handling-datetimeoffset-in-sqlite-with-entity-framework-core/
68
69 // SQLite does not have proper support for DateTimeOffset via Entity Framework Core, see the limitations
70 // here: https://docs.microsoft.com/en-us/ef/core/providers/sqlite/limitations#query-limitations
71 // To work around this, when the Sqlite database provider is used, all model properties of type DateTimeOffset
72 // use the DateTimeOffsetToBinaryConverter
73 // Based on: https://github.com/aspnet/EntityFrameworkCore/issues/10784#issuecomment-415769754
74 // This only supports millisecond precision, but should be sufficient for most use cases.
75 if (!designTime)
76 foreach (var entityType in modelBuilder.Model.GetEntityTypes())
77 {
78 var properties = entityType
79 .ClrType
80 .GetProperties()
81 .Where(p => p.PropertyType == typeof(DateTimeOffset) || p.PropertyType == typeof(DateTimeOffset?));
82 foreach (var property in properties)
83 modelBuilder
84 .Entity(entityType.Name)
85 .Property(property.Name)
86 .HasConversion(new DateTimeOffsetToBinaryConverter());
87 }
88 }
89 }
90}
Configuration options for the Database.DatabaseContext.
string? ConnectionString
The connection string for the database.
DatabaseType DatabaseType
The Configuration.DatabaseType to create.
Backend abstract implementation of IDatabaseContext.
SqliteDatabaseContext(DbContextOptions< SqliteDatabaseContext > dbContextOptions)
Initializes a new instance of the SqliteDatabaseContext class.
readonly bool designTime
If the database context is running in design time mode.
static void ConfigureWith(DbContextOptionsBuilder options, DatabaseConfiguration databaseConfiguration)
Configure the SqliteDatabaseContext.
override void OnModelCreating(ModelBuilder modelBuilder)
DatabaseType
Type of database to user.