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

  • SEARCH
  • Home
  • 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 8378131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:51:24+00:00 2026-06-09T15:51:24+00:00

I’m endeavouring to create a site with many models. Should I have individual contexts,

  • 0

I’m endeavouring to create a site with many models. Should I have individual contexts, or should I just have one? I’ve already put all my Dbsets in the umbrella context I made, just so that Add-Migration and Update-Database actually works. (cause it can only handle 1 context).

I’ve noticed each strongly typed controller has a instance of its associated model context. Now I’m wondering if I should just have one open context (my umbrella superclass context) permanetly open for a page life cycle. It seems way too inefficient to keep creating new instances of a context and disposing them (inside local methods) all the time. What is the recommended way of going about this?

  • 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-09T15:51:25+00:00Added an answer on June 9, 2026 at 3:51 pm

    It is better to have one context track many entities. The same context can then track all entities and it will be useful when using a join query and updating entities that have foreign key relationship. Also you can use fluent API to configure the entities in the DBContext method “OnModelCreating”. If the context doesn’t have them all then configure entity relationships becomes difficult. Grouping entities under different context will eventually affect code maintenance. I like fluent API way to configure entities which keeps the model clean and future proof.

    The DBContext has Dispose method that you can override. We can make better use of it. Besides DBContext creates a database context from where is it called which is absolutely fine. That’s what we want, isn’t it? A incoming request comes as a thread which creates a DBContext( a database context) and then query the database, fetches model and then populates view which is then returned to user.

    The important thing is using the “using” statement for DBContext which will instantly dispose the context after scope is out of the using brace. If you do this then you have no trouble with context living through the request life cycle.

    using (MyDbContext dbCtx = new MyDbContext())
     {
         //fetch the model
     } 
    //here the context is disposed.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

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.