2013-03-28 67 views
0

我有一个存储过程,当我将其映射到EF时,它将返回具有可连接的数据选择,为该存储过程创建复杂类型模型。我是否可以更改此复杂类型,以获取存储过程返回的具有更多列的另一个模型类?或者它必须返回相同数量的列?将存储过程映射到EF中的复杂类型

感谢

例如,我含有S存储过程返回一个选择的客户:

CREATE PROCEDURE [dbo].[CustomersSelect] 
AS 

    SELECT [CustomerID] 
      ,[CompanyName] 
      ,[ContactName] 
      ,[ContactTitle] 
      ,[Address] 
      ,[City]  
     FROM [Northwind].[dbo].[Customers] 

我导入EF和,而不是使用特定的复杂类型此存储过程,我想其映射到Customer类

public partial class Customer 
    { 
     public Customer() 
     { 
      this.Orders = new HashSet<Order>(); 
     } 

     public string CustomerID { get; set; } 
     public string CompanyName { get; set; } 
     public string ContactName { get; set; } 
     public string ContactTitle { get; set; } 
     public string Address { get; set; } 
     public string City { get; set; } 
     public string Region { get; set; } 
     public string PostalCode { get; set; } 
     public string Country { get; set; } 
     public string Phone { get; set; } 
     public string Fax { get; set; } 

     public virtual ICollection<Order> Orders { get; set; } 
    } 

在控制器我尝试使用这个存储过程:

public ActionResult About() 
{ 
     var model = context.CustomersSelect(); 
     return View(model); 
} 

当我运行这个关于()的动作,我收到此错误:

The data reader is incompatible with the specified 'EfMvc4Model.Customer'. A member of the type, 'Region', does not have a corresponding column in the data reader with the same name.

这个任何帮助吗?

谢谢

+0

你试过了什么?你能展示一些你想要做的事情的代码吗? –

+0

我只想知道定义的复杂类型是否与从存储过程返回的结果完全相同。 – tbag

+0

我发送一个具体的例子 – tbag

回答

0

不幸的是,你不能做你想做的事情。如果有两个单独的存储过程返回两个不同的数据集(即使其中一个是另一个的超集),则需要使用两个单独的复杂类型来映射这些存储过程。

正如你已经看到自己:当您尝试“再利用”的复杂类型的第二个存储过程(返回较少的列),你会遇到一个错误是这样的:

System.Data.EntityCommandExecutionException was unhandled

The data reader is incompatible with the specified 'xxxxxxx'. A member of the type, 'ABC', does not have a corresponding column in the data reader with the same name.
Source=System.Data.Entity

和你的第二个电话将永远不会工作。我不明白你怎么能解决这个问题 - 除了使用两个不同的独立复杂类型,完全匹配存储过程的输出“形状”。

相关问题