C#C
C#2y 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 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;
     }
 }
Was this page helpful?