Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I share dbContext without CodeFirst in EntityFramework 4.1?
    text
    copied!<p>Long time lurker, first time poster.</p> <p>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.</p> <p>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.</p> <p>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 </p> <pre><code>using (var db = new MYDB()){ var bar = db.Foo .Include("Transactions") .Include("blah") .Where(...); //do stuff db.SaveChanges(); } </code></pre> <p>One thing I had to do was track all fields that changed, so I abstracted back a level and now we have </p> <pre><code>FooObject bar = GetFooObject(...); bar.Title = "asdfasdf"; //do stuff to bar bar.Save(); </code></pre> <p>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.</p> <p>As it turns out they also do lots of sub-queries based on Transactions and blah. So when they do something like </p> <pre><code>var bar = GetFooObject(...); var t = new Transaction(); //do stuff to t ... bar.Transactions.Add(t); bar.Save(); </code></pre> <p>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.</p> <p>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:</p> <pre><code>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 =&gt; tt.Description) .SingleOrDefault(tt =&gt; tt.TypeID == transactionTypeId), employeeId, comment); </code></pre> <p>But with this code I get an error that I have no definition for TransactionTypes. It doesn't recognize any of my db Objects.</p> <p>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?</p> <h2><strong>EDIT</strong></h2> <p>Here are some things I've found since opening this question. Maybe one of the two will do the trick.</p> <p>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.</p> <p><a href="http://blogs.msdn.com/b/adonet/archive/2009/06/10/poco-in-the-entity-framework-part-3-change-tracking-with-poco.aspx" rel="nofollow noreferrer">poco in the entity framework part-3: change tracking with poco</a></p> <p>And I just found this <a href="https://stackoverflow.com/questions/2030665/how-do-i-share-a-data-context-across-various-model-repositories-in-asp-net-mvc-u">how do I share a data context across various model repositories in asp.net</a> which might be a simple way to approach it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload