Add config file

This commit is contained in:
2023-04-15 21:11:27 +02:00
parent ca30f12f5a
commit 7c7b3ed561
8 changed files with 59 additions and 14 deletions

View File

@ -3,6 +3,7 @@ using Elwig.Models;
namespace Elwig.Helpers {
public class AppDbContext : DbContext {
public DbSet<Country> Countries { get; set; }
public DbSet<Member> Members { get; set; }
public DbSet<BillingAddress> BillingAddresses { get; set; }
@ -22,7 +23,7 @@ namespace Elwig.Helpers {
public DbSet<WineVar> WineVarieties { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
optionsBuilder.UseSqlite("Data Source=\"C:\\Users\\lorenz\\Desktop\\wgprod.sqlite3\"; foreign keys=true");
optionsBuilder.UseSqlite($"Data Source=\"{App.Config.DatabasePath}\"; Foreign Keys=True; Mode=ReadWrite; Cache=Default");
optionsBuilder.UseLazyLoadingProxies();
base.OnConfiguring(optionsBuilder);
}

37
Elwig/Helpers/Config.cs Normal file
View File

@ -0,0 +1,37 @@
using System;
using System.Text;
using IniParser;
using IniParser.Model;
namespace Elwig.Helpers {
public class Config {
private readonly string FileName;
public string DatabasePath;
public Config(string filename) {
FileName = filename;
Read();
}
public void Read() {
var parser = new FileIniDataParser();
IniData? ini = null;
try {
ini = parser.ReadFile(FileName, Encoding.UTF8);
} catch {}
if (ini == null || !ini.TryGetKey("database.path", out string db)) {
DatabasePath = App.DataPath + "database.sqlite3";
} else if (db.Length > 1 && db[1] == ':') {
DatabasePath = db;
} else {
DatabasePath = App.DataPath + db;
}
}
public void Write() {
throw new NotImplementedException();
}
}
}