2012-11-22 59 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;