DbContext Cleanup

This commit is contained in:
Andre Beging
2022-05-21 17:16:40 +02:00
parent f23f225098
commit e71ef30641

View File

@@ -8,69 +8,40 @@ namespace FoodsharingSiegen.Server.Data
/// The fs context class (a. beging, 21.05.2022) /// The fs context class (a. beging, 21.05.2022)
/// </summary> /// </summary>
/// <seealso cref="DbContext"/> /// <seealso cref="DbContext"/>
public class FsContext : DbContext public sealed class FsContext : DbContext
{ {
#region Public Properties #region Public Properties
/// <summary> /// <summary>
/// Gets or sets the value of the interactions (ab) /// Gets or sets the value of the interactions (ab)
/// </summary> /// </summary>
public DbSet<Interaction> Interactions { get; set; } public DbSet<Interaction>? Interactions { get; set; }
/// <summary> /// <summary>
/// Gets or sets the value of the prospects (ab) /// Gets or sets the value of the prospects (ab)
/// </summary> /// </summary>
public DbSet<Prospect> Prospects { get; set; } public DbSet<Prospect>? Prospects { get; set; }
/// <summary> /// <summary>
/// Gets or sets the value of the users (ab) /// Gets or sets the value of the users (ab)
/// </summary> /// </summary>
public DbSet<User> Users { get; set; } public DbSet<User>? Users { get; set; }
#endregion #endregion
#region Setup/Teardown #region Setup/Teardown
/// <summary>
/// Initializes a new instance of the <see cref="FsContext"/> class
/// </summary>
public FsContext() :
base()
{
OnCreated();
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="FsContext"/> class /// Initializes a new instance of the <see cref="FsContext"/> class
/// </summary> /// </summary>
/// <param name="options">The options (ab)</param> /// <param name="options">The options (ab)</param>
public FsContext(DbContextOptions<FsContext> options) : public FsContext(DbContextOptions<FsContext> options) : base(options)
base(options)
{ {
OnCreated(); Database.EnsureCreated();
} }
#endregion #endregion
#region Override OnConfiguring
/// <summary>
/// Ons the configuring using the specified options builder (a. beging, 21.05.2022)
/// </summary>
/// <param name="optionsBuilder">The options builder</param>
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured ||
(!optionsBuilder.Options.Extensions.OfType<RelationalOptionsExtension>().Any(ext => !string.IsNullOrEmpty(ext.ConnectionString) || ext.Connection != null) &&
!optionsBuilder.Options.Extensions.Any(ext => !(ext is RelationalOptionsExtension) && !(ext is CoreOptionsExtension))))
{
}
base.OnConfiguring(optionsBuilder);
}
#endregion
#region Public Method HasChanges #region Public Method HasChanges
/// <summary> /// <summary>
@@ -79,19 +50,7 @@ namespace FoodsharingSiegen.Server.Data
/// <returns>The bool</returns> /// <returns>The bool</returns>
public bool HasChanges() public bool HasChanges()
{ {
return ChangeTracker.Entries().Any(e => e.State == Microsoft.EntityFrameworkCore.EntityState.Added || e.State == Microsoft.EntityFrameworkCore.EntityState.Modified || e.State == Microsoft.EntityFrameworkCore.EntityState.Deleted); return ChangeTracker.Entries().Any(e => e.State is EntityState.Added or EntityState.Modified or EntityState.Deleted);
}
#endregion
#region Private Method OnCreated
/// <summary>
/// Ons the created (a. beging, 21.05.2022)
/// </summary>
private void OnCreated()
{
// Database.EnsureCreated();
} }
#endregion #endregion