csharp DbContextQueryExtension允许从DbContext查询数据(EntityFramework 5+)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp DbContextQueryExtension允许从DbContext查询数据(EntityFramework 5+)相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Linq;


/// <summary>
/// DbContextQueryExtension allows to query data from DbContext (EntityFramework 5+)
/// Latest version is here: https://gist.github.com/pmunin/0733f3bffa24ad2257ac
/// </summary>
public static class DbContextQueryExtensions
{
    /// <summary>
    /// Generates ObjectQuery as described here https://msdn.microsoft.com/library/bb738512(v=vs.100).aspx
    /// </summary>
    /// <param name="context"></param>
    /// <param name="efQuery">EntityFramework SQL (not native)</param>
    /// <returns></returns>
    public static ObjectQuery<DbDataRecord> ObjectQuery(this DbContext context, string efQuery)
    {
        var oc = (context as IObjectContextAdapter).ObjectContext;
        var queryStr = efQuery;
        return new ObjectQuery<DbDataRecord>(queryStr, oc);
    }

    /// <summary>
    /// Generates ObjectQuery of DbDataRecord and translates it to any object (including anonymous)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="context"></param>
    /// <param name="efQuery">EntityFramework SQL (not native)</param>
    /// <param name="selector"></param>
    /// <returns></returns>
    public static IEnumerable<T> ObjectQuery<T>(this DbContext context, string efQuery, Func<DbDataRecord, T> selector)
    {
        var q = ObjectQuery(context, efQuery);
        return q.Select(selector);
    }

    /// <summary>
    /// Executes native SQL query on connection and translates results using specified delegate
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="context"></param>
    /// <param name="sqlQuery">native sql query for DbCommand</param>
    /// <param name="selector">defines translation of DataReader object to target object</param>
    /// <returns></returns>
    public static IEnumerable<T> SqlQuery<T>(this DbContext context, string sqlQuery, Func<DbDataReader, T> selector)
    {
        using (var cmd = context.Database.Connection.CreateCommand())
        {
            cmd.CommandText = sqlQuery;
            var state = cmd.Connection.State;
            cmd.Connection.Open();
            try
            {
                cmd.CommandType = CommandType.Text;
                using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                    while (reader.Read())
                    {
                        yield return selector(reader);
                    }
            }
            finally
            {
                if (state == ConnectionState.Closed)
                    cmd.Connection.Close();
            }
        }
    }
}

以上是关于csharp DbContextQueryExtension允许从DbContext查询数据(EntityFramework 5+)的主要内容,如果未能解决你的问题,请参考以下文章

csharp 例如-CSHARP-GroupDocs.Conversion.Examples.CSharp渲染,RenderPSDImageAsPath.cs

csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs

csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs

csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs

csharp 例如-CSHARP-GroupDocs.Search.Examples.CSharp索引,AddDocumentToIndex.cs

csharp 例如-CSHARP-GroupDocs.Search.Examples.CSharp索引,AddDocumentToIndexAsynchronously.cs