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();
The thing is, when the code tries to execute a sentence:
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:
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.
Julen source share