将 EF POCO 与 SQL Server Compact 4 结合使用

发布于 2024-11-19 07:31:11 字数 2448 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守护在此方 2024-11-26 07:31:11

从您的代码中,我看到您正在使用 IQueryable。这意味着当视图迭代 IQueryable 时,ObjectContext 已经被释放,因此您无法访问数据库。请注意,当您使用 IQueryable 时,在迭代数据库时会从数据库中获取数据(通过示例中的 foreach )。

    IList<BoletimSemanal> boletins;

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

试试这个,它应该可以工作。

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.

末骤雨初歇 2024-11-26 07:31:11

尝试传递 IList 而不是 IQueryable。
您可以将 .ToList() 添加到 linq 查询的末尾:

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

Try to pass a IList instead the IQueryable.
You can do that adding a .ToList() to the end of the linq query:

boletins = (from boletim in db.BoletinsSemanais
            where boletim.Week == week
            && boletim.Year == DateTime.Now.Year
            select boletim).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文