2012-11-22 60 views
0

使用EntityFramework,我創建了一個包含(SELECT)存儲過程的實體模型。我添加了函數導入來獲取SP映射到複雜類型。這適用於返回多個記錄的查詢。實體框架:無法轉換類型錯誤

然而,對於一個查詢,只返回一個記錄,我目前正在此錯誤:

Cannot convert type 'System.Data.Objects.ObjectResult<DAL.FullResponse>' to 'DAL.FullResponse' 

我不知道多樣性的問題,但除了這個問題查詢接受事實一個行標識符作爲參數,沒有關於它強制單行結果(事實上,當我運行導入功能嚮導它說我會返回一個集合的複雜類型

我也不'不知道這是否相關,但我已經在EntityFramew的版本之間切換在這個項目上工作的時候。嘗試使用EF5導致的問題後,我切換到EF4(直到現在)沒有問題。

相關代碼: ASPX

<asp:DetailsView ID="DetailsView1" DataKeyNames="ResponseID" runat="server" 
    AutoGenerateRows="True" Height="250px" Width="350px" BorderWidth="1px" 
    BorderStyle="Dashed" BorderColor="YellowGreen" CellPadding="0" CellSpacing="7" 
    GridLines="None" /> 

CS

protected void ShowData() 
{  
    int respID = Convert.ToInt32(Request.QueryString.Get(0)); 
    System.Diagnostics.Debug.Print("looking for ID: {0}", respID); 

    var responseDetailed = context.GetFullResponse(respID); 
    // get the data 
    DetailsView1.DataSource = (FullResponse)responseDetailed; 
} 

ShowData從Page_Load中

稱爲

有人能指出我的這種固定的方式?我一直堅持了將近一天。

+0

爲什麼你投'(FullResponse)responseDetailed;'? –

+0

因爲它被聲明爲var。如果我將該行更改爲'FullResponse responseDetailed = context.GetFullResponse(respID);'併除去轉換,錯誤是相同的,除了現在說不能**隱式**轉換類型 – mcalex

回答

0

試試這個:

var responseDetailed = context.GetFullResponse(respID); 
    // get the data 
    DetailsView1.DataSource = (IEnumerable<FullResponse>)responseDetailed;