【发布时间】:2011-10-01 15:03:47
【问题描述】:
我正在使用 SQL CE 和 EF Poco 4 开发应用程序。
以下代码
上下文
public class Context : DbContext
{
public DbSet<BoletimSemanal> BoletinsSemanais { get; set; }
public Context()
: base("name=DefaultConnection")
{
}
}
型号
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; }
}
控制器
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>
查看
@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>
foreach出现如下错误:
ObjectContext 实例已被 处置,不能再用于 需要连接的操作。
描述:未处理的异常 在执行过程中发生 当前的网络请求。请查看 堆栈跟踪以获取有关的更多信息 错误及其起源 代码。
异常详情: System.ObjectDisposedException: ObjectContext 实例已 处置,不能再用于 需要连接的操作。
来源错误:
【问题讨论】:
标签: asp.net-mvc-3 entity-framework-4 linq-to-entities sql-server-ce poco