将 EF POCO 与 SQL Server Compact 4 结合使用
我正在使用 SQL CE 和 EF Poco 4 开发一个应用程序。
以下代码
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>
查看
@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.ObjectDisposeException: ObjectContext 实例已 已处置且不能再用于 需要连接的操作。
来源错误:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您的代码中,我看到您正在使用 IQueryable。这意味着当视图迭代 IQueryable 时,ObjectContext 已经被释放,因此您无法访问数据库。请注意,当您使用 IQueryable 时,在迭代数据库时会从数据库中获取数据(通过示例中的 foreach )。
试试这个,它应该可以工作。
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).
Try this and it should work.
尝试传递 IList 而不是 IQueryable。
您可以将 .ToList() 添加到 linq 查询的末尾:
Try to pass a IList instead the IQueryable.
You can do that adding a .ToList() to the end of the linq query: