简体   繁体   English

具有多个结果集和自定义实体的实体框架代码优先存储过程

[英]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

As per MSDN : 根据MSDN

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). 这意味着类型ProductSearchResultProductSizeResult必须是映射类型(实体类型)。 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.

相关问题 实体框架存储过程 - 使用 CodeFirst 的多个结果集 - Entity Framework Stored Procedures - Multiple Result sets with CodeFirst 具有多个结果集的实体框架5存储过程 - Entity Framework 5 stored procedure with multiple result sets Entity Framework Code First是否支持存储过程? - Does Entity Framework Code First support stored procedures? 先在Entity Framework代码中自动生成存储过程 - Generate stored procedures automatically in Entity Framework code-first 首先在实体框架代码中更改CUD存储过程的命名约定 - Change naming convention of CUD stored procedures in entity framework code first 实体框架代码首先使用复合主键映射存储过程 - Entity framework code first map stored procedures with composite primary keys 在Entity Framework数据库优先方法中,如何从存储过程返回多个结果集? - In Entity Framework database-first approach how to return multiple result sets from a stored procedure? 实体框架和存储过程 - Entity Framework & Stored Procedures 实体框架4 +存储过程 - Entity Framework 4 + stored procedures 实体框架不支持从动态查询或临时表构建结果集的存储过程 - Entity Framework doesn't support stored procedures which build result sets from Dynamic queries or Temporary tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM