我對.NET中的實體框架有疑問。如何避免「ObjectContext實例已被處置且不能再用於需要連接的操作」錯誤?
我有一個簡單的ASP.NET網頁,必須用這個功能插入SQL Server的一些數據:
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();
}
的事情是,當代碼嘗試了一句:
// Bind the enabled technolgy with the simulator
T002_SIMULATORS tSimulator = entitites.T002_SIMULATORS.First(p => p.id == enabled.simulatorId);
它給我的錯誤:
該ObjectContext實例已被處置,不能再用於需要連接的操作。
我發現,與其他相關的問題,以爲當它被傳遞給語句中的方法實體對象(entities
)的連接關閉:
// Build the session and web session
T008_SESSIONS tWebSession = ((WebSession)session).Insert(entitites);
的Insert
功能爲實現:
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;
}
的好消息是,這Insert
功能封裝代碼,我可以把它移動到的地方WH ich被稱爲。所以我的問題是:如果有一個地方我會被迫調用這個函數呢?那我怎麼能避免這個錯誤。通過參數作爲參考(我試過並沒有工作)
非常感謝您提前!
Julen。
謝謝!真的,我看不到它! – Julen 2011-03-30 12:36:46