Reputation: 207
I have been working on a project that uses Entity Framework (database first) with Visual Studio 2012. It is ASP.NET MVC project (C#),razor. In a View I have a Kendo Grid:
@(Html.Kendo().Grid<RunSummary>()
.Name("CheckedPatients")
.DataSource(datasource => datasource
.Ajax().PageSize(25)
.ServerOperation(false)
.Sort(sort => sort.Add("TimeOn").Descending())
.Read(read => read.Action("GetRunSummaries", "PatientReport")))
.Columns(columns =>
{
columns.Bound(c => c.UniqueId).Title(ELSORegistry.Resources.Views.Home.HomeStrings.UniqueId)
.ClientTemplate("<input type='checkbox' class='primaryBox' id='#= UniqueId #' value='#= UniqueId #'>#= UniqueId #</input>");
columns.Bound(c => c.RunNo).Title(SharedStrings.Run);
columns.Bound(c => c.Birthdate).Title(SharedStrings.Birthdate).Format("{0:g}").Filterable(true);
columns.Bound(c => c.customAge).Title(SharedStrings.Age)
.Filterable(
filterable => filterable
.UI("AgeFilter")
.Extra(false)
.Operators(operators => operators
.ForString(str => str.Clear().IsEqualTo("Is equal to"))
)
);
columns.Bound(c => c.TimeOn).Title(PatientStrings.DateOn)
.Format("{0:g}")
.Filterable(true);
columns.Bound(c => c.TimeOff).Title(PatientStrings.DateOff)
.Format("{0:g}")
.Filterable(true);
columns.Bound(c => c.DischargedAlive).Title(PatientStrings.DischargedAlive).Filterable(true);//.ClientTemplate("#= DischargedAlive ? 'Yes' : 'No' #");
columns.Bound(c => c.ShowSubmitted).Title(PatientStrings.Submitted).Filterable(true);//.ClientTemplate("#= ShowSubmitted ? 'Yes' : 'No' #");
columns.Bound(c => c.SupportTypeEnum).Title(PatientStrings.SupportType).Filterable(true);//.ClientTemplate("#= SupportType ? 'Yes' : 'No' #");
}
)
.Pageable(p => p.PageSizes(new[] {10, 25, 50, 100}))
.Sortable()
.Filterable( )
.Events( e => e.FilterMenuInit("FilterMenuFuncWithAge") ) // apply x [closing box] on pop up filter box
)
As a source of the Kendo Grid, I used a stored procedure:
CREATE PROCEDURE [dbo].[IGD_spPatientListReportFillGrid]
(@CenterId uniqueidentifier)
AS
BEGIN
SET NOCOUNT ON
-- DECLARE @start DATETIME, @end DATETIME;
-- SET @start = GETDATE();
SELECT R.RunId, R.submittedDate, R.CompletedBy, R.isRunLocked, R.LockDate, P.isPatientLocked, DATEDIFF(DAY, ISNULL
(R.submittedDate, GETDATE()), GETDATE())
AS DaysDiff, CP.CenterId, P.PatientId,
P.UniqueId,
R.RunNo,
R.SupportType,
CAST(P.Birthdate AS DATETIME) AS Birthdate,
P.Sex, P.Race,
R.Discontinuation,
R.DischargedAlive,
CAST(R.AdmitDate AS DATETIME) AS AdmitDate,YEAR(R.TimeOn) AS Year,
CAST(R.TimeOn AS DATETIME) AS TimeOn,
CAST(ISNULL(R.TimeOff,
(SELECT TOP (1) EndTime
FROM ECLS.RunDetails AS RD
WHERE (RunId = R.RunId) AND (NOT (EndTime IS NULL))
ORDER BY EndTime DESC)) AS DATETIME2) AS TimeOff,
CAST(R.DischargeDate AS DATETIME) AS DischargeDate,
CAST(R.DeathDate AS DATETIME) AS DeathDate,
DATEDIFF(day, CAST(P.Birthdate AS DATETIME), CAST(R.TimeOn AS DATETIME)) AS Age,
R.CompletedDate,
CASE WHEN (R.CompletedBy IS NULL) OR
((R.TimeOn IS NULL) OR
(R.TimeOff IS NULL)) OR
(R.CompletedBy IS NOT NULL AND ((R.TimeOn IS NULL) OR
(R.TimeOff IS NULL))) THEN 'false' ELSE 'true' END AS IsCompleted,
dbo.ELSO_IGD_CalculateAge(DATEDIFF(day, P.Birthdate,R.TimeOn)) AS customAge,
CASE WHEN (R.CompletedBy IS NULL) OR
((R.TimeOn IS NULL) OR
(R.TimeOff IS NULL)) OR
(R.CompletedBy IS NOT NULL AND ((R.TimeOn IS NULL) OR
(R.TimeOff IS NULL))) THEN 'false' ELSE 'true' END AS ShowSubmitted
FROM ECLS.Runs AS R INNER JOIN
Registry.Patients AS P ON R.PatientId = P.PatientId INNER JOIN
Registry.CenterPatients CP ON P.PatientId = CP.PatientId
WHERE CP.CenterId = @CenterId
ORDER BY R.TimeOn;
END;
I imported the stored procedure by using "Update model from database", and the destination type is entity "RunSummary" (Kendo Grid uses it as a model). The procedure that communicates with the model is:
public List<RunSummary> GetRunSummariesForPatientReportGrid(Guid? centerId)
{
using (var context = new ELSORegistryEntities())
{
return context.IGD_spPatientListReportFillGrid(centerId).ToList<RunSummary>();
}
}
The code in controller is:
public JsonResult GetRunSummaries([ELSORegistry.Helpers.CustomDataSourceRequest] DataSourceRequest request)
{
var center = Session["Center"] as Center;
var centerId = center != null && center.CenterNo != 0 ? center.CenterId : (Guid?)null;
List<RunSummary> myList = new Repository().GetRunSummariesForPatientReportGrid(centerId);
return Json(myList.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
My problem is in that the Kendo Grid doesn't show anything, it is empty. In the browser, in Console, I found the error: "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection". (I want to mention that the field ShowSubmitted is implemented in model exstensions of RunSummary, as:
public bool ShowSubmitted
{
get
{
return this.LookForSubmitted();
}
}
private bool LookForSubmitted()
{
bool retVal = false;
retVal = (new Repository()).GetSubmitted(this.RunId);
return retVal;
}
public bool GetSubmitted(Guid? runId = null)
{
using (var context = new ELSORegistryEntities())
{
context.Configuration.ProxyCreationEnabled = false;
RunSummary run = context.RunSummaries.FirstOrDefault<RunSummary>(p => p.RunId == runId);
bool submitted = false;
if (run.IsCompleted == "true")
{
submitted = true;
}
else
{
submitted = false;
}
return submitted;
}
}
Any idea, how to solve the problem? Thank you in advance for any help.
Upvotes: 1
Views: 786
Reputation: 1275
Try disabling lazy loading by adding the following code snippet to your DbContext class. By default, Entity Framework lazily loads data, meaning data is not loaded until it is requested.
public AlenanDBEntities(): base("name=AlenanDBEntities")
{
this.Configuration.LazyLoadingEnabled = false;
}
If you do not want to disable lazy loading for all of your entities, you can enable eagerly loading by declaring a property using the virtual key word. Not knowing what your entities look like, you would add something like this
public class Run
{
public virtual List<RunSummary> RunSummaries { get; set; }
}
Upvotes: 1