asp.net mvc 3 - Using EF POCO with SQL Server Compact 4 -
asp.net mvc 3 - Using EF POCO with SQL Server Compact 4 -
i developing application sql ce , ef poco 4.
the next 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 boletim.week == week && boletim.year == datetime.now.year select boletim); } homecoming 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 next error occurs in foreach:
the objectcontext instance has been disposed , can no longer used operations require connection.
description: unhandled exception occurred during execution of current web request. please review stack trace more info error , originated in code.
exception details: system.objectdisposedexception: objectcontext instance has been disposed , can no longer used operations require connection.
source error:
from code, see using iqueryable. means when view iterates iqueryable, objectcontext disposed, can't access database. note when using iqueryable info db fetched when iterating on (throug foreach in example).
ilist<boletimsemanal> boletins; boletins = (from boletim in db.boletinssemanais boletim.week == week && boletim.year == datetime.now.year select boletim).tolist();
try , should work.
asp.net-mvc-3 entity-framework-4 linq-to-entities sql-server-ce poco
Comments
Post a Comment