0

My question is about creating tables through dbcontext options.

In short, I'm developing a reusable base dbContext for future uses and I have some tables, that sometimes I would like to use, other times I would not.

For example I have a translations table set for storing different translations in different languages, if I would like to use it, I just could do something like this:

using var context = new CustomDbContext(optionsBuilder
    .AddTranslations()
    .Options);

How can I achieve this?

Another idea: I saw from this post that one should use different dbContext objects for different kinds of tables. For example I should have a translations context, identity context (for login and such) and so on.

Should one use this approach instead of the optionsbuilder one?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

If you want to add/remove Entities from a model, the basic strategy is to add the entities in OnModelCreating, which runs once per DbContext type, by default. And to omit the DbSet<TEntity> property and use DbContext.Set<TEntity> to access the Entity.

It doesn't really matter how you push the configuration into the DbContext, and what's convenient depends on what kind of app and what framework.

If you need different tables in the same application, you can change the model caching behavior to cause OnModelCreating to run multiple times for the same DbContext type. See Dynamic Models Sample.

David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67