Compare commits
2
Commits
6ed8c0e231
...
2cd5d4aeed
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2cd5d4aeed | ||
|
|
decc46d84a |
@@ -0,0 +1,166 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Logof_Client;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Zentrale Datenbankverwaltung für Logof Client.
|
||||||
|
///
|
||||||
|
/// Zuständig für:
|
||||||
|
/// - PostgreSQL-Verbindung
|
||||||
|
/// - EF Core DbContext
|
||||||
|
/// - Datenbankmodelle
|
||||||
|
/// - Migrationen
|
||||||
|
/// - Verbindungstest
|
||||||
|
/// </summary>
|
||||||
|
public static class DatabseService
|
||||||
|
{
|
||||||
|
private static DbContextOptions<LogofDbContext>? _options;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialisiert die Datenbankverbindung.
|
||||||
|
/// Sollte einmal beim Programmstart aufgerufen werden.
|
||||||
|
/// </summary>
|
||||||
|
public static void Initialize(string connectionString)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(connectionString))
|
||||||
|
throw new ArgumentException(
|
||||||
|
"Kein Datenbank-Connection-String angegeben.",
|
||||||
|
nameof(connectionString));
|
||||||
|
|
||||||
|
var builder = new DbContextOptionsBuilder<LogofDbContext>();
|
||||||
|
|
||||||
|
builder.UseNpgsql(connectionString);
|
||||||
|
|
||||||
|
_options = builder.Options;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Erstellt einen neuen DbContext.
|
||||||
|
/// </summary>
|
||||||
|
public static LogofDbContext GetContext()
|
||||||
|
{
|
||||||
|
if (_options == null)
|
||||||
|
throw new InvalidOperationException(
|
||||||
|
"DB wurde noch nicht initialisiert. " +
|
||||||
|
"Rufen Sie DB.Initialize(...) auf.");
|
||||||
|
|
||||||
|
return new LogofDbContext(_options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Testet die Verbindung zum Datenbankserver.
|
||||||
|
/// </summary>
|
||||||
|
public static async Task<bool> TestConnectionAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await using var db = GetContext();
|
||||||
|
return await db.Database.CanConnectAsync();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Erstellt bzw. aktualisiert das Datenbankschema
|
||||||
|
/// anhand der vorhandenen EF-Core-Migrationen.
|
||||||
|
/// </summary>
|
||||||
|
public static async Task MigrateAsync()
|
||||||
|
{
|
||||||
|
await using var db = GetContext();
|
||||||
|
|
||||||
|
await db.Database.MigrateAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// DATABASE CONTEXT
|
||||||
|
// ============================================================================
|
||||||
|
|
||||||
|
public class LogofDbContext : DbContext
|
||||||
|
{
|
||||||
|
public DbSet<CustomerEx> Customers => Set<CustomerEx>();
|
||||||
|
|
||||||
|
public LogofDbContext(
|
||||||
|
DbContextOptions<LogofDbContext> options)
|
||||||
|
: base(options)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
base.OnModelCreating(modelBuilder);
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------
|
||||||
|
// Customer
|
||||||
|
// --------------------------------------------------------------------
|
||||||
|
|
||||||
|
modelBuilder.Entity<CustomerEx>(entity =>
|
||||||
|
{
|
||||||
|
entity.ToTable("customers");
|
||||||
|
|
||||||
|
entity.HasKey(x => x.Id);
|
||||||
|
|
||||||
|
entity.Property(x => x.Id)
|
||||||
|
.ValueGeneratedOnAdd();
|
||||||
|
|
||||||
|
entity.Property(x => x.Name)
|
||||||
|
.HasMaxLength(255)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
entity.Property(x => x.Description)
|
||||||
|
.HasMaxLength(4000);
|
||||||
|
|
||||||
|
entity.Property(x => x.SenderAddress)
|
||||||
|
.HasMaxLength(4000);
|
||||||
|
|
||||||
|
entity.Property(x => x.Separator)
|
||||||
|
.HasMaxLength(1)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
// Optimistic Concurrency
|
||||||
|
entity.Property(x => x.Version)
|
||||||
|
.IsConcurrencyToken();
|
||||||
|
|
||||||
|
entity.Property(x => x.CreatedAt)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
entity.Property(x => x.UpdatedAt)
|
||||||
|
.IsRequired();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// DATABASE MODELS
|
||||||
|
// ============================================================================
|
||||||
|
|
||||||
|
public class CustomerEx
|
||||||
|
{
|
||||||
|
public long Id { get; set; }
|
||||||
|
|
||||||
|
public string Name { get; set; } = "";
|
||||||
|
|
||||||
|
public string Description { get; set; } = "";
|
||||||
|
|
||||||
|
public string SenderAddress { get; set; } = "";
|
||||||
|
|
||||||
|
public char Separator { get; set; } = ',';
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Wird bei jeder Änderung erhöht.
|
||||||
|
/// Verhindert, dass zwei Benutzer sich gegenseitig
|
||||||
|
/// Änderungen überschreiben.
|
||||||
|
/// </summary>
|
||||||
|
public long Version { get; set; } = 1;
|
||||||
|
|
||||||
|
public DateTime CreatedAt { get; set; }
|
||||||
|
|
||||||
|
public DateTime UpdatedAt { get; set; }
|
||||||
|
}
|
||||||
+4
-1
@@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>WinExe</OutputType>
|
<OutputType>WinExe</OutputType>
|
||||||
<TargetFramework>net9.0</TargetFramework>
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
|
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
|
||||||
<ApplicationManifest>app.manifest</ApplicationManifest>
|
<ApplicationManifest>app.manifest</ApplicationManifest>
|
||||||
@@ -24,6 +24,9 @@
|
|||||||
<PackageReference Include="Markdown.Avalonia" Version="11.0.3" />
|
<PackageReference Include="Markdown.Avalonia" Version="11.0.3" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||||
<PackageReference Include="PdfSharp" Version="6.1.1" />
|
<PackageReference Include="PdfSharp" Version="6.1.1" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.12" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.12" />
|
||||||
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user