How to avoid the "ObjectContext instance has been deleted and can no longer be used for operations requiring a connection" error?

I have doubts about the Entity Framework in .NET.

I have a simple ASP.NET webpage that needs to insert some data into SQL Server using this function:

public void InsertSimulation() { icarus_portalEntities entitites = new icarus_portalEntities(); // Build the Simulation T004_SIMULATIONS tSimulation = T004_SIMULATIONS.CreateT004_SIMULATIONS(0, name_, creationDate_, gsoapPort_, endTimeStep_, scenarioX_, scenarioY_, penetrationRates_.wifi, penetrationRates_.gprs, penetrationRates_.wimax, penetrationRates_.lte, penetrationRates_.mcn, penetrationRates_.edge, penetrationRates_.hsdpa, totalNumberOfNodes_, "RandomWalkMobility", ((RandomWalkMobility)mobility_).vehicularSpeed, ((RandomWalkMobility)mobility_).angleVariation, ((RandomWalkMobility)mobility_).cellRadius, ((RandomWalkMobility)mobility_).decorrelation, ((RandomWalkMobility)mobility_).minimumDistance, false); // Bind the Simulation with the FK of user T001_USERS tUser = entitites.T001_USERS.First(p => p.user_name == createdBy_); tUser.T004_SIMULATIONS.Add(tSimulation); // Bind the Simulation with the FK of the crmm T003_CRRM tCrmm = entitites.T003_CRRM.First(p => p.name == crmm_.Name); tCrmm.T004_SIMULATIONS.Add(tSimulation); // Add the created sessions to the simulation foreach (SimulationSession session in SimulationSession.createdSessions_) { if (session.GetType() == typeof(WebSession)) { // Build the session and web session T008_SESSIONS tWebSession = ((WebSession)session).Insert(entitites); // Bind the session to the simulation tSimulation.T008_SESSIONS.Add(tWebSession); } } // Add the enanabled technologies to the simulator foreach (EnabledTechnologies enabled in enabledTechnologies_) { // Build the enabled technology T005_ENABLED_TECHNOLOGIES tEnabled = T005_ENABLED_TECHNOLOGIES.CreateT005_ENABLED_TECHNOLOGIES(0, enabled.centerX, enabled.centerY, enabled.radius, false); // Bind the enabled technolgy with the simulator T002_SIMULATORS tSimulator = entitites.T002_SIMULATORS.First(p => p.id == enabled.simulatorId); tSimulator.T005_ENABLED_TECHNOLOGIES.Add(tEnabled); // Bind the enabled technolgoy with the simulation tSimulation.T005_ENABLED_TECHNOLOGIES.Add(tEnabled); } entitites.SaveChanges(); } 

The thing is, when the code tries to execute a sentence:

 // Bind the enabled technolgy with the simulator T002_SIMULATORS tSimulator = entitites.T002_SIMULATORS.First(p => p.id == enabled.simulatorId); 

This gives me an error:

The ObjectContext instance has been deleted and can no longer be used for operations that require a connection.

I found with other related questions that somehow the connection of the Entity ( entities ) object is closed when it is passed to the method in the statement:

 // Build the session and web session T008_SESSIONS tWebSession = ((WebSession)session).Insert(entitites); 

The Insert function is implemented as:

 public T008_SESSIONS Insert(icarus_portalEntities entities) { // Create new session object with data T008_SESSIONS tSession = T008_SESSIONS.CreateT008_SESSIONS(0, name_, periodicty_, false); using (entities) { // Create new web session object with data T010_SESSIONS_WEB tSessionWeb = T010_SESSIONS_WEB.CreateT010_SESSIONS_WEB(0, (int)lambda_, (int)pagesPerSession_, (int)objectsSize_.alpha, (int)objectsSize_.beta, (int)objectsPerWebPage_.alpha, (int)objectsPerWebPage_.beta, (int)timeBetweenPages_.alpha, (int)timeBetweenPages_.beta, (int)timeBetweenObjects_.alpha, (int)timeBetweenObjects_.beta, false); // Add to the session the web session (bind FK) tSession.T010_SESSIONS_WEB.Add(tSessionWeb); // Add session to the rest of the entities entities.AddToT008_SESSIONS(tSession); // Commit all the data to the database entities.SaveChanges(); //var tempSession = entities.T008_SESSIONS.First(n => n.Equals(tSession.id)); //tempSession.T010_SESSIONS_WEB.Add(tSessionWeb); } return tSession; } 

The good news is that the code that encapsulates Insert , I can move it to the called place. So my question is: What if there is a place where I would have to call a function? How then could I avoid a mistake. Passing a parameter as a reference (I tried and did not work)

Thank you in advance!

Julen.

+4
source share
2 answers

What if there is a place in which I would be forced to call to function? How then could I avoid a mistake. Passing a parameter as a link (I tried and did not work)

The problem is your Insert() function, which you wrap entities in using(){} statements.

 using (entities) //calls dispose { } 

When usage ends, it is called on its objects and closes your connection.

Your context must remain active throughout the life of our unit of work. Ideally, your using statement should be in InsertSimulation() , because from the code that it represents is your unit of work for you entitites .

+10
source

If you look at the public method T008_SESSIONS Insert (icarus_portalEntities objects)

you pass a reference to your object object and because you use the using statement

 using (entities) {} 

he will position the object

+2
source

Source: https://habr.com/ru/post/1345866/


All Articles