Невозможно привести объект типа «System.Data.Objects.MaterializedDataRecord». - PullRequest
3 голосов
/ 28 декабря 2010

У меня есть GridView, связанный с EntityDataSource.

EntityDataSource имеет внутренний параметр для параметров Где. До этого момента все работало нормально.

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

Я использую Event RowDataBound Entity Framework для извлечения значения для каждой отдельной строки и выполнения некоторой логики.

Как только я запускаю код, я получаю эту ошибку:

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

Мне кажется, что при добавлении параметров в EntityDataSource smt меняется, поэтому я не могу использовать EF, как раньше Любая идея? Спасибо, ребята!


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

1 Ответ

1 голос
/ 17 октября 2012

Прочтите сообщение Диего Веги в блоге о привязке EntityDataSource в событии RowDataBound:

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

Вы сталкиваетесь с четвертым сценарием его «правил упаковки».

Наконец, если вы установите свойство Select для проецирования (т. Е. «It.CustomerID, it.CustomerName», вы получите DbDataRecord независимо от того, как вы начали свой запрос.

Вы не сможете получить исходную сущность. Вы должны обрабатывать свой 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. Попробуйте использовать сценарий 1 или 2 правил Диего для упаковки.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...