Skip to content
Orbis Alonzo Gutierrez edited this page Aug 23, 2023 · 1 revision

Others

This page contains the documentations referents to users helpers, functions, or implementations

EFCoreConfiguration

Contains a simple query filter for isDeleted=true entities.

Example usage:

public class PersonEFCoreConfiguration : EFCoreConfiguration<Person>
{
    public override void ConfigureEF(EntityTypeBuilder<Person> builder)
    {

    }
}


//in your db context
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        //apply all ef configurations and filters
        modelBuilder.ApplyConfigurationsFromAssembly(typeof(EFCoreConfiguration<>).Assembly);
    }

MigrationDbContextConfigAssembly

This class permit work in migrations with deferments' schemas how parameter in migrations, is used with IDbContextSchema interface.

//this interface need implemented in the dbcontext for usage this way
public interface IDbContextSchema
{
    string Schema { get; }
}

public class ApplicationDbContext : FoundationKitDbContext, IDbContextSchema
{
    public string Schema { get; set; }
   
    //is a example for management deferents' schema with appsettings.json
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options,
        IOptions<DatabaseOption> databaseOption) : base(options)
    {
        Schema = databaseOption?.Value?.Schema ?? "dbo";
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema(Schema);
    }
}

  //you need a database config how this

   services.AddDbContext<ApplicationDbContext>(opt =>
        {
            string schema = configuration.GetSection(DatabaseOption.Key)[nameof(DatabaseOption.Schema)] ?? "dbo";

            opt.UseSqlServer(configuration.GetConnectionString("Database"),
                 x => x.MigrationsHistoryTable(HistoryRepository.DefaultTableName, schema))
            .ReplaceService<IMigrationsAssembly, MigrationDbContextConfigAssembly>();
        });

Very important for this work, you need to update your generated migrations example:

//migration autogenerated and updated for support schema

namespace Domain.Migrations
{
    /// <inheritdoc />
    public partial class Initialize : Migration
    {
         //here
        private readonly IDbContextSchema _schema;
        //here
        public Initialize(IDbContextSchema schema)
        {
            _schema = schema ?? throw new ArgumentNullException(nameof(schema));
        }

        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {

            migrationBuilder.CreateTable(
                schema: _schema.Schema,  //here
                name: "Users",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                    Email = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false),
                    HasConsoleAccess = table.Column<bool>(type: "bit", nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false),
                    CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    UpdatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Users", x => x.Id);
                });
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            

            migrationBuilder.DropTable(
                schema: _schema.Schema,  //here

                name: "Users");
        }
    }
}

Clone this wiki locally