2010-12-28 6 views
3

GridView를 EntityDataSource에 연결했습니다.'System.Data.Objects.MaterializedDataRecord'형식의 개체를 캐스팅 할 수 없습니다.

EntityDataSource에는 Where Parameters에 대한 내부 매개 변수가 있습니다. 이 시점까지 모든 것이 잘 작동합니다.

<asp:EntityDataSource ID="EntityDataSourceListAuthors" runat="server" ConnectionString="name=CmsConnectionStringEntityDataModel" 
     DefaultContainerName="CmsConnectionStringEntityDataModel" EnableFlattening="False" 
     EntitySetName="CmsAuthors" EntityTypeFilter="" OrderBy="it.FirstName" Select="it.AuthorId, it.UserId, it.FirstName, it.LastName, it.NoteInternal, it.ContentAuthor" 
     Where="it.UserId = @ActiveUser"> 
    </asp:EntityDataSource> 

나는 이벤트는 모든 단일 행에 대한 값을 검색하고 일부 로직을 실행하는 엔티티 프레임 워크를 RowDataBound 사용합니다. EntityDataSource의 SMT에 매개 변수를 추가 할 때

Unable to cast object of type 'System.Data.Objects.MaterializedDataRecord' to type 'WebProject.DataAccess.DatabaseModels.CmsAuthor'. 

그것은 나에게 보인다 내가 어떤 생각하기 전에로 EF을 사용할 수없는 나는 그렇게 변화 :

는 최대한 빨리 코드를 실행 나는이 오류가 나타납니다? 고마워! RowDataBound 이벤트 EntityDataSource 바인딩에


 protected void uxListAuthorsDisplayer_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
      switch (e.Row.RowType) 
      { 
       // In case type of row is DataRow (a data row of GridView) 
       case DataControlRowType.DataRow: 
        // Display friendly User's Name instead of his Guid 
        // Retrive underlying data from a single row rappresented in GridView (use Entity Framwork)     
        WebProject.DataAccess.DatabaseModels.CmsAuthor myRow = (WebProject.DataAccess.DatabaseModels.CmsAuthor)e.Row.DataItem; 
        // Retrive the Guid for a User in a specific row 
        Guid myUserGuid = (Guid)myRow.UserId; 
        // Find out used UserName using Guid UserId 
        MembershipUser mySelectedUser = Membership.GetUser(myUserGuid); 
        // Write friendly User's Name instead of his Guid value in a specific Grid View Cell 
        e.Row.Cells[3].Text = mySelectedUser.UserName; 

        // Disable Delete Button if a Content has associated an Author 
        // Use Entity Framwork for retriving data - Create a "Context" for a single Row 
        using (CmsConnectionStringEntityDataModel context = new CmsConnectionStringEntityDataModel()) 
        { 
         // Find out Row Id and create an varaible to store it 
         int myWorkingRowId = myRow.AuthorId; 
         // Find the Edit Link 
         HyperLink myEditLink = (HyperLink)e.Row.FindControl("uxLinkEditButton"); 
         // Find the Delete Button 
         LinkButton myDeleteButton = (LinkButton)e.Row.FindControl("uxLinkDeleteButton"); 
         // Find the System Note Label 
         Label mySystemNote = (Label)e.Row.FindControl("uxSystemNoteDisplayer"); 
         // Use of Lamba Espression with EF to check if an Author is associated with a Content 
         CmsContent authorIdInContent = context.CmsContents.FirstOrDefault(x => x.AuthorId == myWorkingRowId); 
         // Make visible or invisible the Delete Button if an Author is associated to a Content 
         if (authorIdInContent != null) 
         { 
          myDeleteButton.Visible = false; 
          mySystemNote.Text = "Author is being used in Contents"; 
         } 
         else 
         { 
          myDeleteButton.Visible = true; 
         } 

         // Programmatically Limiting Functionality depending on User's Roles 
         myEditLink.Visible = User.IsInRole("CMS-ADMINISTRATOR") || User.IsInRole("CMS-AUTHOR") || User.IsInRole("CMS-EDITOR"); 
         myDeleteButton.Visible = User.IsInRole("CMS-ADMINISTRATOR"); 
        } 
        break; 
      } 
     } 
+0

코드를 감쌀 수 없습니다. –

+0

이것에 대한 아이디어가 있으십니까? – GibboK

답변

1

읽기 디에고 베가의 블로그 게시물 :

http://blogs.msdn.com/b/diego/archive/2008/05/13/entitydatasource-to-wrap-or-not-to-wrap.aspx

당신은 "포장을위한 규칙"자신의 네 번째 시나리오로 실행하고 있습니다.

Finally, if you set the Select property to do a projection (i.e. "it.CustomerID, it.CustomerName", you get DbDataRecord regardless of how you start your query.

소스 엔티티를 가져올 수 없습니다. DataItem을 DataRow와 같은 것으로 취급해야합니다.

If e.Row.RowType = DataControlRowType.DataRow Then 

    Dim rowCmsAuthor = CType(e.Row.DataItem, Data.Common.DbDataRecord) 

    Dim myUserID As Integer = rowCmsAuthor("UserId") 

End If 

또는 Select 속성을 EntityDataSource에서 제거 할 수도 있습니다. 포장에 Diego의 규칙 1 또는 2를 사용하십시오.

관련 문제