2016-08-16 17 views
1

Visual Studio 2012でEntity Framework(データベースを最初に使用)を使用しているプロジェクトで作業しています。これはASP.NET MVCプロジェクトですC#)、カミソリ。私は剣道グリッドを有するビューで:私は「データベースから更新モデル」を使用してストアドプロシージャをインポートObjectContextインスタンスが破棄され、接続エラーが発生する操作に使用できなくなる

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; 

@(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 
     ) 

剣道グリッドのソースとして、私は、ストアドプロシージャを使用しました、目的地タイプは実体 "RunSummary"です(剣道グリッドをモデルとして使用します)。モデルと通信手順は次のとおりです。

public List<RunSummary> GetRunSummariesForPatientReportGrid(Guid? centerId) 
     { 
      using (var context = new ELSORegistryEntities()) 
      { 
       return context.IGD_spPatientListReportFillGrid(centerId).ToList<RunSummary>(); 
      } 
     } 

コントローラのコードされています。私の問題は、剣道グリッドは、それが空で、何も表示しないことである

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); 

     } 

。ブラウザで、Consoleで、「ObjectContextインスタンスが破棄され、接続が必要な操作には使用できなくなりました」というエラーが見つかりました。 ?問題を解決する方法

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; 
      } 

     } 

任意のアイデア、任意の助けを事前にいただきありがとうございます

答えて

1

lazy loadingを無効にしてみてください:(私はとして、フィールドShowSubmittedはRunSummaryのモデルexstensionsに実装されていることを言及したいと思います。あなたのDbContextクラスに次のコードスニペットを追加する。デフォルトですることにより、Entity Frameworkのはいい加減にそれが要求されるまでデータがロードされていないという意味、データをロードします。

public AlenanDBEntities(): base("name=AlenanDBEntities") 
{ 
    this.Configuration.LazyLoadingEnabled = false; 
} 

あなたのすべてのために遅延読み込みを無効にしたくない場合エンティティ、あなたenaすることができますeagerly loadingは、仮想キーワードを使用してプロパティを宣言することによって実現します。エンティティの外観を知らない場合は、このようなものを追加してください。

public class Run 
{ 
    public virtual List<RunSummary> RunSummaries { get; set; } 
} 
+0

ご協力いただきありがとうございます。 RunSummaryオブジェクトに仮想プロパティPatientSummaryがあり、取得しようとしたときに問題が見つかりました(ストアドプロシージャがデータセットを返す場所はありません)。 '[System.Web.Script.Serialization.ScriptIgnore]パブリック仮想PatientSummary PatientSummary {get;セット; } 'Entity FrameworkのオブジェクトPatientSummaryを回避する方法は?助けをありがとうございました。 – alenan2013

関連する問題