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.
I would leave behind the object context stuff.
The way I achieve a shared DBContext in my MVC app is like this:
Then whenever I need to do a database operation I can call:
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.