[英]Entity Framework Code First Stored Procedures with Multiple Result Sets and custom entity
My stored procedure returns two sets of result. 我的存储过程返回了两组结果。 ProductSearchResult and ProductSizeResult are not entities in my case, hence i get an exception The EntitySet name 'Database.ProductSearchResult' could not be found. 在我的情况下,ProductSearchResult和ProductSizeResult不是实体,因此出现异常。找不到EntitySet名称“ Database.ProductSearchResult”。
I don't want to create Entity in dbcontext for each of my procedure results, is there any solution to map stored procedure to custom objects. 我不想为每个过程结果在dbcontext中创建Entity,是否有任何解决方案可将存储过程映射到自定义对象。
try
{
DbContext.Database.Connection.Open();
DbDataReader reader = cmd.ExecuteReader();
result = ((IObjectContextAdapter)DbContext).ObjectContext.Translate<ProductSearchResult>(reader, "ProductSearchResult", MergeOption.AppendOnly).ToList();
reader.NextResult();
productSizeResults = ((IObjectContextAdapter)DbContext).ObjectContext.Translate<ProductSizeResult>(reader, "ProductSizeResult", MergeOption.AppendOnly).ToList();
}
catch (Exception ex)
{
log.Error(ex);
}
finally
{
DbContext.Database.Connection.Close();
}
My custom entities, 我的自定义实体
public class ProductSearchResult
{
public int Id { get; set; }
public string Name { get; set; }
public int AvailableQuantity { get; set; }
public int Price{ get; set; }
}
public class ProductSizeResult
{
public int Id { get; set; }
public string Size { get; set; }
public int Count { get; set; }
}
My Stored Procedure, 我的存储过程,
ALTER PROC GetProductResult @PrimaryCategory nvarchar(100)
AS
select P.Id
,P.Name
,PI.AvailableQuantity
,PI.Price
from Products P
inner join ProductInventories PI on P.Id = PI.ProductId
--where clause
select CA.Name Size,count(1) Count
from Products P
inner join ProductInventories PI on P.Id = PI.ProductId
inner join CustomAttributes CA on PI.CustomAttributeID = CA.Id
--where clause
group by CA.Name
The Translate<TElement> method enables you to execute a standard ADO.NET query against a data source and translate the returned data rows into entity objects. Translate <TElement>方法使您可以对数据源执行标准的ADO.NET查询,并将返回的数据行转换为实体对象。
(my emphasis) (我的重点)
That means that the types ProductSearchResult
and ProductSizeResult
must be mapped types (entity types). 这意味着类型ProductSearchResult
和ProductSizeResult
必须是映射类型(实体类型)。 This fact was already revealed --more or less-- by the MergeOption
parameter. MergeOption
参数已经或多或少地揭示了这一事实。 That is about how to add the objects to the change tracker, which doesn't make sense for non-entity types. 那是关于如何将对象添加到变更跟踪器,这对于非实体类型没有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.