c# - 无法转换类型为 'System.Data.Objects.MaterializedDataRecord' 的对象

标签 c# asp.net entity-framework

我有一个与 EntityDataSource 相连的 GridView。

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>

我使用 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;
            }
        }

最佳答案

阅读 Diego Vega 关于在 RowDataBound 事件中绑定(bind) EntityDataSource 的博文:

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

或者,您可以从 EntityDataSource 中删除 Select 属性。尝试使用 Diego 规则的场景 1 或场景 2 进行包装。

关于c# - 无法转换类型为 'System.Data.Objects.MaterializedDataRecord' 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4545156/

相关文章:

c# - 如何动态判断是否使用了 windows 或 forms 身份验证

c# - C# 中用于捕获委托(delegate)的循环反汇编的无用变量?

c# - 从类实例将消息返回到表单的最佳方法?

ASP.NET MVC4 代码优先 - 'Cannot attach the file as database' 异常

c# - 建模多态关联数据库优先与代码优先

asp.net - 我什么时候应该在 ASP.NET 网站中使用服务总线?

asp 下拉列表中 onchange 事件的 JavaScript

c# - 如何自定义 TreeNode 在 TreeView 中的显示顺序/位置?

c# - 通过 ID 获取对象,例如 Entity Framework 中的 SQL WHERE 谓词

entity-framework - Entity Framework - 与假外键的关系(数据库中没有外键)