Extract sqlite conntection to AppDbContext

This commit is contained in:
2023-08-24 10:23:16 +02:00
parent 3a12837706
commit 5074f945cf
2 changed files with 23 additions and 11 deletions

View File

@ -6,6 +6,8 @@ using System.IO;
using System;
using System.Windows;
using Microsoft.Extensions.Logging;
using Microsoft.Data.Sqlite;
using System.Text.RegularExpressions;
namespace Elwig.Helpers {
public class AppDbContext : DbContext {
@ -60,7 +62,20 @@ namespace Elwig.Helpers {
}
SavedLastWriteTime = LastWriteTime;
SavedChanges += OnSavedChanges;
}
public static SqliteConnection Connect() {
var cnx = new SqliteConnection(ConnectionString);
cnx.CreateFunction<string, string?, bool?>("REGEXP", (pattern, value) => value == null ? null : Regex.Match(value, pattern).Success, true);
cnx.Open();
return cnx;
}
public static async Task<SqliteConnection> ConnectAsync() {
var cnx = new SqliteConnection(ConnectionString);
cnx.CreateFunction<string, string?, bool?>("REGEXP", (pattern, value) => value == null ? null : Regex.Match(value, pattern).Success, true);
await cnx.OpenAsync();
return cnx;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {