C
C#3w 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 ツ3w 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
Angius3w ago
Why tho
Rowin ツ
Rowin ツ3w ago
What do you mean?
Angius
Angius3w ago
Why do those tables even have suffixes
Rowin ツ
Rowin ツ3w ago
Because our company said so
Angius
Angius3w ago
Ah, rip
Rowin ツ
Rowin ツ3w ago
Yup
Angius
Angius3w ago
Well, in any case, it seems fine to me
Rowin ツ
Rowin ツ3w 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
Angius3w 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 ツ3w ago
i though that was a suffix but yeah that is the case
Angius
Angius3w ago
https://stackoverflow.com/a/12967044/6042255 prefix_foo_suffix Seems you should be able to just use a convention
Rowin ツ
Rowin ツ3w ago
I need need a new context every call with supplied prefix
FusedQyou
FusedQyou3w 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 ツ3w ago
What do you mean exactly?
FusedQyou
FusedQyou3w 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 ツ3w 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
FusedQyou3w ago
Yeah pretty much
Want results from more Discord servers?
Add your server
More Posts
✅ I need help with compiling an console application exe with the Roslyn compilerHi there, ive been working on a funky little program for fun, and its basically supposed to be a wei✅ I need help with positioning the news under weather, stock, and time. https://paste.ofcode.org/v3i tried by using Cursorposition but it didn't work for some reason.Put a pdf in a formI want to select a pdf from the pc and format it in a button or something. When I press the button t✅ EF Core - Not getting updated data from PostgreSQL tableI have the following method in my `DbService.cs` ```csharp public async Task<long?> GetLastDumpTimeSHow do I implement the Chrome API Sidepanel with a Blazor browser extension?Posted the question with more details here: https://stackoverflow.com/questions/78640477/how-do-i-u✅ Need Help making a Loader for a game.I just need someone to tell me what to Fix. 2 Erros and 1 warrning not really sure what to do and YeQuestPDF Why does the document become empty after adding the table and how to fix it?I have a pdf file that I generate using code, I'm trying to add a table to it that is contained in c✅ [Solved] Dotnet restore is failing on new appsI'm working with the CLI on linux. Any new app I create (console, web api, etc) running dotnet restoI am new to C# and would like to hear about all the resources(paid or not) where I can learn C#.The title kind of explains it all. If you suggest paid content, Please put up how much the program/cpng image but background is not transparentCan somebody help me out? The original image is attached