Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9273739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:11:04+00:00 2026-06-18T16:11:04+00:00

As it says on the tin, is it possible to create an instance of

  • 0

As it says on the tin, is it possible to create an instance of a DbContext suitable for use as an Entity Framework 5.0 POCO context where the properies that are normally declared as

public DbSet<T> Entities { get; set; }

aren’t set/known until runtime?

I’d like to make a repository that has methods like

public TEntity Find<TEntity>(object key) where TEntity : class
{
    return _context.Set<TEntity>().Find(key);
}

public void Update<TEntity>(TEntity entity) where TEntity : class
{
    if (_context.Entry(entity).State == EntityState.Detached) //entity is detached 
        _context.Set<TEntity>().Attach(entity);
    _context.Entry(entity).State = EntityState.Modified;
}

.... etc

And then use them like:

Widget w = repository.Find<Widget>(123);
repository.SaveChanges();

This is trivial if the repository’s context is set to a class that contains a DbSet<Widget> Widgets, but can it be done such that the entity types that I plan to use won’t be know until runtime OR possibly not until I actually USE them? So that if I have a new class Foo, I can immediately query my repository to .Find<Foo>(123) without having to first add a DbSet<Foo> Foos to my DbContext class?

I think this should be possible because there’s nothing special about the poco classes or the DbContext instance which holds references to them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:11:05+00:00Added an answer on June 18, 2026 at 4:11 pm

    You don’t need DbSet<Foo> Foos property in your context. That is just one way to tell context about existence of the Foo entity. There are multiple ways how context discovers mapped entities:

    • By explicit DbSet<T> properties
    • By navigation properties in already discovered entities
    • By specifying mapping in DbModelBuilder
      • Overriding OnModelCreated in your context type
      • Creating DbModelBuilder manually, building it and compiling it into DbCompiledModel which can be passed to DbContext constructor
      • By declaring EntityTypeConfiguration<T> for each entity and adding it to DbModelBuilder (either in OnModelCreated or in manually created DbModelBuilder).

    The last option can be used to discover all required entities at application startup (for example by searching assemblies for all entity configuration classes and registering them to model builder) but it is still not fully dynamic solution. Compiled model is normally constructed only once per application run when the context is used for the first time. Without replacing the compiled model you cannot add or remove mapped entity types to the context.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Exactly what it says on the tin. Is there a builtin python function that
What it says on the tin: I'd like to use the @property / @synthesize
Like it says on the tin, I've a launch script that is supposed to
Pretty much what it says on the tin. I've tried googling around but can't
says if i develop a Ruby on Rails application using Rails 2.3.2, will that
Firefox says they support 128px cursors, but that you must keep within 32x32px for
What it says on the tin. All I want to do is save an
As the questions says I have always believed that a strongly typed list is
Consider the following code (from a requirement that says that 3 is special for
What it says on the tin.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.