C
C#6mo ago
Rowin ツ

Database suffix builder

Question, is doing something like this acceptable?
public class EditorDbContext : DbContext
{
private readonly string _tableSuffix;

public DbSet<EditorItem> EditorItems { get; set; }

public EditorDbContext(DbContextOptions<EditorDbContext> options, string tableSuffix)
: base(options)
{
_tableSuffix = tableSuffix ?? throw new ArgumentNullException(nameof(tableSuffix));
}

protected override void OnModelCreating(ModelBuilder builder)
{
builder.ApplyConfiguration(new EditorItemConfiguration(_tableSuffix));
}
}
public class EditorDbContext : DbContext
{
private readonly string _tableSuffix;

public DbSet<EditorItem> EditorItems { get; set; }

public EditorDbContext(DbContextOptions<EditorDbContext> options, string tableSuffix)
: base(options)
{
_tableSuffix = tableSuffix ?? throw new ArgumentNullException(nameof(tableSuffix));
}

protected override void OnModelCreating(ModelBuilder builder)
{
builder.ApplyConfiguration(new EditorItemConfiguration(_tableSuffix));
}
}
public class EditorItemConfiguration : IEntityTypeConfiguration<EditorItem>
{
private string _suffix;
public EditorItemConfiguration(string suffix)
{
_suffix = suffix;
}
public void Configure(EntityTypeBuilder<EditorItem> builder)
{
builder.ToTable($"tblplantekstobjects{_suffix}");
}
}
public class EditorItemConfiguration : IEntityTypeConfiguration<EditorItem>
{
private string _suffix;
public EditorItemConfiguration(string suffix)
{
_suffix = suffix;
}
public void Configure(EntityTypeBuilder<EditorItem> builder)
{
builder.ToTable($"tblplantekstobjects{_suffix}");
}
}
Though I think is is right I'm still getting some dependency injection problems but im not sure if i should spend time on fixing those or if there is a different way For example this:
public async Task<List<EditorItemDto>> GetEditorTree(int regelingId)
{
string tableSuffix = "3";
using (var dbContext = _dbContextFactory.CreateDbContext(tableSuffix))
{
var items = await dbContext.EditorItems.ToListAsync();
Console.WriteLine($"Entities from EditorItem_{tableSuffix}: {items.Count}");
var list = _mapper.Map<List<EditorItemDto>>(items);
return list;
}
}
public async Task<List<EditorItemDto>> GetEditorTree(int regelingId)
{
string tableSuffix = "3";
using (var dbContext = _dbContextFactory.CreateDbContext(tableSuffix))
{
var items = await dbContext.EditorItems.ToListAsync();
Console.WriteLine($"Entities from EditorItem_{tableSuffix}: {items.Count}");
var list = _mapper.Map<List<EditorItemDto>>(items);
return list;
}
}
18 Replies
Rowin ツ
Rowin ツOP6mo ago
public interface IEditorDbContextFactory
{
EditorDbContext CreateDbContext(string tableSuffix);

}
public interface IEditorDbContextFactory
{
EditorDbContext CreateDbContext(string tableSuffix);

}
public class EditorDbContextFactory : IEditorDbContextFactory
{
private readonly DbContextOptions<EditorDbContext> _options;

public EditorDbContextFactory(DbContextOptions<EditorDbContext> options)
{
_options = options;
}

public EditorDbContext CreateDbContext(string tableSuffix)
{
return new EditorDbContext(_options, tableSuffix);
}
}
public class EditorDbContextFactory : IEditorDbContextFactory
{
private readonly DbContextOptions<EditorDbContext> _options;

public EditorDbContextFactory(DbContextOptions<EditorDbContext> options)
{
_options = options;
}

public EditorDbContext CreateDbContext(string tableSuffix)
{
return new EditorDbContext(_options, tableSuffix);
}
}
Angius
Angius6mo ago
Why tho
Rowin ツ
Rowin ツOP6mo ago
What do you mean?
Angius
Angius6mo ago
Why do those tables even have suffixes
Rowin ツ
Rowin ツOP6mo ago
Because our company said so
Angius
Angius6mo ago
Ah, rip
Rowin ツ
Rowin ツOP6mo ago
Yup
Angius
Angius6mo ago
Well, in any case, it seems fine to me
Rowin ツ
Rowin ツOP6mo ago
Same, yet
InvalidOperationException: Error while validating the service descriptor 'ServiceType: EditorApi.DbContexts.EditorDbContext Lifetime: Scoped ImplementationType: EditorApi.DbContexts.EditorDbContext': Unable to resolve service for type 'System.String' while attempting to activate 'EditorApi.DbContexts.EditorDbContext'.
InvalidOperationException: Error while validating the service descriptor 'ServiceType: EditorApi.DbContexts.EditorDbContext Lifetime: Scoped ImplementationType: EditorApi.DbContexts.EditorDbContext': Unable to resolve service for type 'System.String' while attempting to activate 'EditorApi.DbContexts.EditorDbContext'.
Angius
Angius6mo ago
I guess it's trying to do constructor injection somewhere, where you pass the suffix to a constructor Seems like what you're doing is adding a prefix to all tables, rather than suffix, right? Like, tblplantekstobjectsUsers, tblplantekstobjectsBlogposts, etc?
Rowin ツ
Rowin ツOP6mo ago
i though that was a suffix but yeah that is the case
Angius
Angius6mo ago
https://stackoverflow.com/a/12967044/6042255 prefix_foo_suffix Seems you should be able to just use a convention
Rowin ツ
Rowin ツOP6mo ago
I need need a new context every call with supplied prefix
FusedQyou
FusedQyou6mo ago
What is acceptable? Assigning default suffixes to tables? Why not? Only thing I don't like here is how you inject a primitive type into DI. You should probably have an actual configuration object
Rowin ツ
Rowin ツOP6mo ago
What do you mean exactly?
FusedQyou
FusedQyou6mo ago
Well not DI directly, but as a parameter I would make an actual Configuration object instead I would also really consider using actual DI to get the context. I assume this is an API? You can just call AddDBContext and not use factories because there's no reason to Then you are forced to have a Configuration object 😎
Rowin ツ
Rowin ツOP6mo ago
public EditorDbContext(DbContextOptions<EditorDbContext> options, EditorDbContextConfig config)
: base(options)
{
_tablePrefix = config.TablePrefix ?? throw new ArgumentNullException(nameof(config.TablePrefix));
}
public EditorDbContext(DbContextOptions<EditorDbContext> options, EditorDbContextConfig config)
: base(options)
{
_tablePrefix = config.TablePrefix ?? throw new ArgumentNullException(nameof(config.TablePrefix));
}
You mean something like this? god i keep using suffix but that should be prefix
FusedQyou
FusedQyou6mo ago
Yeah pretty much
Want results from more Discord servers?
Add your server