| | | 1 | | using System.Reflection; |
| | | 2 | | |
| | | 3 | | namespace ZhEaIsNsAaBn.Infrastructure.Core; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Base <see cref="DbContext"/> with the shared model conventions: required <c>CreatedDate</c>, decimals with precision |
| | | 7 | | /// scale 2, restrict-delete foreign keys, <c>PhoneNumber</c> converters, and every <see cref="IEntityTypeConfiguration{ |
| | | 8 | | /// the configuration assembly. Database defaults (<c>CreatedDate</c>, sequential Guid keys) come from the provider pack |
| | | 9 | | /// <c>UseSqlServerInfrastructure()</c> (<c>getdate()</c>, <c>NEWSEQUENTIALID()</c>). Audit fields (<c>UpdatedDate</c> i |
| | | 10 | | /// only by <see cref="AuditInterceptor{TId}"/> (<c>WithAuditing<TId>()</c>). |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// <c>OnModelCreating</c> is sealed. Put model configuration in <see cref="CustomConfiguration"/>. |
| | | 14 | | /// </remarks> |
| | | 15 | | /// <typeparam name="TAppDbContext">The application's context type (for typed <see cref="DbContextOptions{TContext}"/>). |
| | | 16 | | public abstract class BaseDbContext< TAppDbContext > : DbContext |
| | | 17 | | where TAppDbContext : DbContext |
| | | 18 | | { |
| | | 19 | | private readonly Assembly _configurationAssembly; |
| | | 20 | | |
| | | 21 | | |
| | | 22 | | /// <summary>Creates the context; configure the provider in <c>OnConfiguring</c>.</summary> |
| | | 23 | | /// <param name="configurationAssembly">The assembly whose <see cref="IEntityTypeConfiguration{TEntity}"/> classes a |
| | | 24 | | /// <exception cref="ArgumentNullException"><paramref name="configurationAssembly"/> is <see langword="null"/>.</exc |
| | 0 | 25 | | protected BaseDbContext( Assembly configurationAssembly ) |
| | | 26 | | { |
| | 0 | 27 | | ArgumentNullException.ThrowIfNull( configurationAssembly ); |
| | | 28 | | |
| | 0 | 29 | | _configurationAssembly = configurationAssembly; |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary>Creates the context with options from DI.</summary> |
| | | 33 | | /// <param name="options">The context options.</param> |
| | | 34 | | /// <param name="configurationAssembly">The assembly whose <see cref="IEntityTypeConfiguration{TEntity}"/> classes a |
| | | 35 | | /// <exception cref="ArgumentNullException"><paramref name="configurationAssembly"/> is <see langword="null"/>.</exc |
| | 652 | 36 | | protected BaseDbContext( DbContextOptions<TAppDbContext> options, Assembly configurationAssembly ) : base( options ) |
| | | 37 | | { |
| | 652 | 38 | | ArgumentNullException.ThrowIfNull( configurationAssembly ); |
| | | 39 | | |
| | 652 | 40 | | _configurationAssembly = configurationAssembly; |
| | 652 | 41 | | } |
| | | 42 | | |
| | | 43 | | |
| | | 44 | | /// <summary>Override to configure the model. Runs after the shared conventions and before the assembly configuratio |
| | | 45 | | /// <param name="modelBuilder">The model builder.</param> |
| | 0 | 46 | | public virtual void CustomConfiguration( ModelBuilder modelBuilder ) { } |
| | | 47 | | |
| | | 48 | | /// <summary>Stores every enum property by name as Unicode text of 50 (<c>nvarchar(50)</c> on SQL Server). Call it f |
| | | 49 | | /// <param name="modelBuilder">The model builder.</param> |
| | 16 | 50 | | protected virtual void ConfigureEnumsProperties( ModelBuilder modelBuilder ) => BaseDbContextConfiguration.Configure |
| | | 51 | | |
| | | 52 | | /// <summary>Stores every <see cref="ulong"/> property as Unicode text of 20 (<c>nvarchar(20)</c> on SQL Server). Ca |
| | | 53 | | /// <param name="modelBuilder">The model builder.</param> |
| | 16 | 54 | | protected virtual void ConfigureUlongProperties( ModelBuilder modelBuilder ) => BaseDbContextConfiguration.Configure |
| | | 55 | | |
| | | 56 | | /// <summary>Applies the shared conventions, <see cref="CustomConfiguration"/> and the assembly configurations.</sum |
| | | 57 | | /// <param name="modelBuilder">The model builder.</param> |
| | | 58 | | protected sealed override void OnModelCreating( ModelBuilder modelBuilder ) |
| | | 59 | | { |
| | 44 | 60 | | BaseDbContextConfiguration.OnModelCreating( modelBuilder, CustomConfiguration, _configurationAssembly, this.GetM |
| | | 61 | | |
| | | 62 | | |
| | 44 | 63 | | base.OnModelCreating( modelBuilder ); |
| | 44 | 64 | | } |
| | | 65 | | } |