【问题标题】:Using EF POCO with SQL Server Compact 4将 EF POCO 与 SQL Server Compact 4 一起使用
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多