实体框架CTP5-从存储过程中读取多个记录集

问题描述:

在EF4中,这不容易实现.您要么不得不降级到经典的ADO.NET(DataReader),请使用 ObjectContext .翻译 或使用 EFExtensions 项目.

In EF4, this was not easily possible. You either had to degrade to classic ADO.NET (DataReader), use ObjectContext.Translate or use the EFExtensions project.

这是否已经在EF CTP5中实现了?

Has this been implemented off the shelf in EF CTP5?

如果没有,推荐的方法是什么?

If not, what is the recommended way of doing this?

我们是否必须将DbContext<T>强制转换为IObjectContextAdapter并访问基础的ObjectContext才能使用此方法?

Do we have to cast the DbContext<T> as an IObjectContextAdapter and access the underlying ObjectContext in order to get to this method?

有人可以为我指出一篇有关使用EF CTP5做到这一点的好文章吗?

Can someone point me to a good article on doing this with EF CTP5?

所以我可以正常工作了,这就是我所拥有的:

So i got this working, here's what i have:

internal SomeInternalPOCOWrapper FindXXX(string xxx)
{
    Condition.Requires(xxx).IsNotNullOrEmpty();

    var someInternalPokey = new SomeInternalPOCOWrapper();
    var ctx = (this as IObjectContextAdapter).ObjectContext;

    var con = new SqlConnection("xxxxx");
    {
        con.Open();
        DbCommand cmd = con.CreateCommand();
        cmd.CommandText = "exec dbo.usp_XXX @xxxx";
        cmd.Parameters.Add(new SqlParameter("xxxx", xxx));

        using (var rdr = cmd.ExecuteReader())
        {
            // -- RESULT SET #1
            someInternalPokey.Prop1 = ctx.Translate<InternalPoco1>(rdr);

            // -- RESULT SET #2
            rdr.NextResult();
            someInternalPokey.Prop2 = ctx.Translate<InternalPoco2>(rdr);

            // -- RESULT SET #3
            rdr.NextResult();
            someInternalPokey.Prop3 = ctx.Translate<InternalPoco3>(rdr);

            // RESULT SET #4
            rdr.NextResult();
            someInternalPokey.Prop4 = ctx.Translate<InternalPoco4>(rdr);
        }
        con.Close();
    }

    return someInternalPokey;
}

从本质上讲,它基本上类似于经典的ADO.NET.您阅读了DbReader,前进到下一个结果集,等等.

Essentially, it's basically like classic ADO.NET. You read the DbReader, advance to the next result set, etc.

但是至少我们有Translate方法,它似乎在结果集字段和提供的实体之间从左到右.

But at least we have the Translate method which seemingly does a left-to-right between the result set fields and the supplied entity.

请注意,该方法是内部方法.

Note the method is internal.

我的存储库调用此方法,然后将DTO 水合到我的域对象中.

My Repository calls this method, then hydrates the DTO into my domain objects.

我对它不满意的原因有3个:

I'm not 100% happy with it for 3 reasons:

  1. 我们必须将DbContext强制转换为IObjectContextAdapter.方法Translate应该在IMO的DbContext<T>类上.
  2. 我们必须使用经典的ADO.NET对象.为什么?对于任何ORM,存储过程都是必须具有的.我对EF的主要抱怨是缺少存储过程支持,而EF CTP5似乎并未纠正这一点.
  3. 您必须打开一个新的SqlConnection.为什么它不能使用与EF上下文打开的连接相同的连接?
  1. We have to cast the DbContext as IObjectContextAdapter. The method Translate should be on DbContext<T> class IMO.
  2. We have to use classic ADO.NET Objects. Why? Stored Procedures are a must have for any ORM. My main gripe with EF is the lack of the stored procedure support and this seems to not have been rectified with EF CTP5.
  3. You have to open a new SqlConnection. Why can't it use the same connection as the one opened by the EF Context?

希望这不仅可以帮助某人,还可以向EF团队发送消息.我们需要现成的SPROCS多结果支持.您可以将存储的proc映射到复杂类型,为什么我们不能将存储的proc映射到多个复杂类型?

Hope this both helps someone and sends out a message to the EF team. We need multiple result support for SPROCS off the shelf. You can map a stored proc to a complex type, so why can't we map a stored proc to multiple complex types?