Skip to content

Commit

Permalink
fix: Error configuration at default ctor of CasbinDbContext
Browse files Browse the repository at this point in the history
(cherry picked from commit b51e0e0)
  • Loading branch information
sagilio committed Aug 20, 2023
1 parent 8087ab8 commit 203269b
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions Casbin.Persist.Adapter.EFCore/CasbinDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,45 @@ namespace Casbin.Persist.Adapter.EFCore
public partial class CasbinDbContext<TKey> : DbContext where TKey : IEquatable<TKey>
{
public virtual DbSet<EFCorePersistPolicy<TKey>> Policies { get; set; }
private const string DefaultTableName = "casbin_rule";

private readonly IEntityTypeConfiguration<EFCorePersistPolicy<TKey>> _casbinModelConfig;
private readonly string _defaultSchemaName;
private readonly string _schemaName;

public CasbinDbContext()
{
_casbinModelConfig = new DefaultPersistPolicyEntityTypeConfiguration<TKey>(DefaultTableName);
}

public CasbinDbContext(DbContextOptions<CasbinDbContext<TKey>> options, string defaultSchemaName = null, string defaultTableName = "casbin_rule") : base(options)
public CasbinDbContext(DbContextOptions<CasbinDbContext<TKey>> options, string schemaName = null, string tableName = DefaultTableName) : base(options)
{
_casbinModelConfig = new DefaultPersistPolicyEntityTypeConfiguration<TKey>(defaultTableName);
_defaultSchemaName = defaultSchemaName;
_casbinModelConfig = new DefaultPersistPolicyEntityTypeConfiguration<TKey>(tableName);
_schemaName = schemaName;
}

public CasbinDbContext(DbContextOptions<CasbinDbContext<TKey>> options, IEntityTypeConfiguration<EFCorePersistPolicy<TKey>> casbinModelConfig, string defaultSchemaName = null) : base(options)
protected CasbinDbContext(DbContextOptions options, string schemaName = null, string tableName = DefaultTableName) : base(options)
{
_casbinModelConfig = casbinModelConfig;
_defaultSchemaName = defaultSchemaName;
_casbinModelConfig = new DefaultPersistPolicyEntityTypeConfiguration<TKey>(tableName);
_schemaName = schemaName;
}

protected CasbinDbContext(DbContextOptions options, string defaultSchemaName = null, string defaultTableName = "casbin_rule") : base(options)
public CasbinDbContext(DbContextOptions<CasbinDbContext<TKey>> options, IEntityTypeConfiguration<EFCorePersistPolicy<TKey>> casbinModelConfig, string schemaName = null) : base(options)
{
_casbinModelConfig = new DefaultPersistPolicyEntityTypeConfiguration<TKey>(defaultTableName);
_defaultSchemaName = defaultSchemaName;
_casbinModelConfig = casbinModelConfig;
_schemaName = schemaName;
}

protected CasbinDbContext(DbContextOptions options, IEntityTypeConfiguration<EFCorePersistPolicy<TKey>> casbinModelConfig, string defaultSchemaName = null) : base(options)
protected CasbinDbContext(DbContextOptions options, IEntityTypeConfiguration<EFCorePersistPolicy<TKey>> casbinModelConfig, string schemaName = null) : base(options)
{
_casbinModelConfig = casbinModelConfig;
_defaultSchemaName = defaultSchemaName;
_schemaName = schemaName;
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
if (string.IsNullOrWhiteSpace(_defaultSchemaName) is false)
if (string.IsNullOrWhiteSpace(_schemaName) is false)
{
modelBuilder.HasDefaultSchema(_defaultSchemaName);
modelBuilder.HasDefaultSchema(_schemaName);
}

if (_casbinModelConfig is not null)
Expand Down

0 comments on commit 203269b

Please sign in to comment.