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 8985893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:25:32+00:00 2026-06-15T21:25:32+00:00

Long time lurker, first time poster. I’ve found tons of stuff on here about

  • 0

Long time lurker, first time poster.

I’ve found tons of stuff on here about how to share dbContext across repositories using CodeFirst, but I can’t seem to relate that to the project I’m working on, which doesn’t use code first or dependency injection.

First, a little background on the project to make sure that I’m approaching this the right way. I came into this project and they were using EF4 and with a DB first. I’m far from an expert on EF, but I’ve fumbled around with several different projects now.

I’ve had to implement several different requirements that have forced me to intervene between their “service” level and the database. In other words, their objects were making calls directly to the EF db objects like

using (var db = new MYDB()){
  var bar = db.Foo
   .Include("Transactions")
   .Include("blah")
   .Where(...);

  //do stuff

  db.SaveChanges();
}

One thing I had to do was track all fields that changed, so I abstracted back a level and now we have

FooObject bar = GetFooObject(...);
bar.Title = "asdfasdf";
//do stuff to bar
bar.Save();

which wraps up all the fields into properties so I can log out any changes. In bar.save I open a db context, get the existing Foo or create a new one, assign all the values and then call db.SaveChanges.

As it turns out they also do lots of sub-queries based on Transactions and blah. So when they do something like

var bar = GetFooObject(...);

var t = new Transaction();
//do stuff to t
...
bar.Transactions.Add(t);
bar.Save();

I get hit with all kinds of context errors saying that the dbcontext is no longer available etc. Which I totally understand. What I don’t know is how to fix it. I’ve seen lots of stuff about creating a dbContext before it’s used and then passing it in, but I can’t seem to figure out the proper way to do it so it will work with my code.

My most recent attempt based on several examples about how to convert DBContext to ObjectContext (which was in turn based on the fact that all of the examples I found about sharing a connection referenced ObjectContext and not DBContext) looks like this:

using (var db = ((IObjectContextAdapter)(new FooDB())).ObjectContext)
  {
using (var context = new DbContext(db, false))
{
var bar = FooObject.GetFooObject(fooId);
Result r = bar.ProcTrans(amount,
                        transDate,
                            db.TransactionTypes
                              .Include(tt => tt.Description)
                              .SingleOrDefault(tt => tt.TypeID == transactionTypeId),
                            employeeId,
                            comment);

But with this code I get an error that I have no definition for TransactionTypes. It doesn’t recognize any of my db Objects.

How can I create a DBContext and pass it to my FooObject so that I can keep it open for the related updates? I don’t even know if I’m asking the question exactly right. How do I bridge this gap without recoding the whole thing?

EDIT

Here are some things I’ve found since opening this question. Maybe one of the two will do the trick.

Well, this finding certainly is more along the lines of recoding the whole thing but I did find this when looking for links regarding the “do change tracking” with triggers response.

poco in the entity framework part-3: change tracking with poco

And I just found this how do I share a data context across various model repositories in asp.net which might be a simple way to approach it.

  • 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-15T21:25:33+00:00Added an answer on June 15, 2026 at 9:25 pm

    I would leave behind the object context stuff.

    The way I achieve a shared DBContext in my MVC app is like this:

    public class BaseRepository
    {
        public static MyAppContext GetDataContext()
        {
            string ocKey = "ocm_" + HttpContext.Current.GetHashCode().ToString("x");
    
            if (!HttpContext.Current.Items.Contains(ocKey))
                HttpContext.Current.Items.Add(ocKey, new MyAppContext());
    
            return HttpContext.Current.Items[ocKey] as MyAppContext;
        }
    }
    

    Then whenever I need to do a database operation I can call:

    BaseRepository.GetDataContext().YourObjects.Where(x => ...);
    ....
    BaseRepository.GetDataContext().SaveChanges();
    

    As long as you are still in the same HTTP context you will be sharing the same DB Context. Not entirely sure this will eliminate the errors that you are getting, but it’s at least a way to share your context.

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

Sidebar

Related Questions

Long time lurker, first time poster. I have found some good answers on here
Long time lurker, first time poster here. My question is: Using C# 2.0, is
Long term lurker, first time poster here. I have written a class to model
Long time lurker, first time poster. I have a page that can dynamically (with
First time poster, long time lurker :) I have a form setup that posts
Long time lurker, first time poster; hoping anyone can help me out. Im using
Long time lurker, first time poster. I am moving from LESS to Sass, and
Long time lurker, first time poster! Goal My ultimate goal is to make a
long time reader/first time poster here. So I've got a checkbox array that posted
Long time lurcher, first time poster here. I've been grappling with this problem for

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.