Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法强制转换类型为“System.Data.Objects.MaterializedDataRecord”的对象_C#_Asp.net_Entity Framework - Fatal编程技术网

C# 无法强制转换类型为“System.Data.Objects.MaterializedDataRecord”的对象

C# 无法强制转换类型为“System.Data.Objects.MaterializedDataRecord”的对象,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,我有一个与EntityDataSource连接的GridView EntityDataSource具有Where参数的内部参数。到目前为止一切正常 <asp:EntityDataSource ID="EntityDataSourceListAuthors" runat="server" ConnectionString="name=CmsConnectionStringEntityDataModel" DefaultContainerName="CmsConnect

我有一个与EntityDataSource连接的GridView

EntityDataSource具有Where参数的内部参数。到目前为止一切正常

    <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>
在我看来,当向EntityDataSource添加参数时,smt正在改变,所以我不能像以前那样使用EF 有什么想法吗?谢谢大家


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

您将遇到他的包装规则的第四个场景

最后,如果将Select属性设置为进行投影,即it.CustomerID、it.CustomerName,则无论如何启动查询,都会得到DbDataRecord

您将无法获取源实体。您必须将数据项视为数据行

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进行包装。

包装您的代码确实不可读。
        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;
            }
        }
If e.Row.RowType = DataControlRowType.DataRow Then

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

    Dim myUserID As Integer = rowCmsAuthor("UserId")

End If