Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6123999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:04:56+00:00 2026-05-23T16:04:56+00:00

I am developing an application with SQL CE and EF Poco 4. The following

  • 0

I am developing an application with SQL CE and EF Poco 4.

The following code

Context

public class Context : DbContext
{
    public DbSet<BoletimSemanal> BoletinsSemanais { get; set; }

    public Context()
        : base("name=DefaultConnection")
    {
    }
}

Model

public class BoletimSemanal
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateModified { get; set; }
    public int Week { get; set; }
    public int Year { get; set; }
}

Controller

public ActionResult Index()
{
    IQueryable<BoletimSemanal> boletins;
    using (var db = new Context())
    {
        var calInfo = DateTimeFormatInfo.CurrentInfo;
        var week = calInfo.Calendar.GetWeekOfYear(DateTime.Now, calInfo.CalendarWeekRule, calInfo.FirstDayOfWeek);


        boletins = (from boletim in db.BoletinsSemanais
                    where boletim.Week == week
                            && boletim.Year == DateTime.Now.Year
                    select boletim);
    }

    return View(boletins.DefaultIfEmpty());
}

Web.config

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=|DataDirectory|Data.sdf;" providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>

View

@model IEnumerable<SextaIgreja.Web.Models.BoletimSemanal>
@{
    ViewBag.Title = "Downloads";
}
<div id="boletim-semanal" class="column last">
    <p class="title">Boletim Semanal
    @if (Request.IsAuthenticated)
    { 
        @Html.ActionLink("+", "Create", "Downloads", new { @class="add" })
    }
    </p>
    <div class="content border">
        <p>Content</p>
        <ul>
        @foreach (var item in Model)
        {
            <li>@item.Name</li>
        }
        </ul>
    </div>
</div>

the following error occurs in foreach:

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

Description: An unhandled exception
occurred during the execution of the
current web request. Please review the
stack trace for more information about
the error and where it originated in
the code.

Exception Details:
System.ObjectDisposedException: The
ObjectContext instance has been
disposed and can no longer be used for
operations that require a connection.

Source Error:

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:04:56+00:00Added an answer on May 23, 2026 at 4:04 pm

    From your code, I see that you are using IQueryable. This means that when the view iterates that IQueryable, ObjectContext is already disposed, so you can’t access the database. Note that when you are using IQueryable data from db is fetched when you are iterating over it (throug foreach in your example).

        IList<BoletimSemanal> boletins;
    
        boletins = (from boletim in db.BoletinsSemanais
                    where boletim.Week == week
                            && boletim.Year == DateTime.Now.Year
                    select boletim).ToList();
    

    Try this and it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently developing a web application in ASP.Net with SQL Server and I would
I am developing a C#/SQL ASP.NET web application in VS 2008. Currently I am
I am developing a C#/SQL ASP.NET web application in VS 2008. I want the
I've recently started developing my first serious application which uses a SQL database, and
I'm developing an ASP.NET application with VB, and using SQL Command and Connection in
I am developing large data collecting ASP.Net/Windows service application-pair that uses Microsoft SQL Server
I am developing application that works with sql server, I read and update data
I am developing my application in asp.net 3.5 and sql server 2005, and I
I'm developing an application that reads data from a SQL server database (migrated from
I am developing an application which needs to backup data between SQL Express 2005

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.