实体框架中原始查询的通用实体

时间:2015-05-14 16:35:29

标签: c# entity-framework entity-framework-6

我正在使用EF,我必须执行一些用户可以根据需要创建的自定义查询。

我知道对于原始查询,我必须使用SqlQuery,但我不知道查询返回的对象。

我使用的是UnitOfWork模式:

public class Repository<TEntity> : IRepository<TEntity> where TEntity:class
{
    CellularFiveLocalEntities context = null;
    public Repository()
    {
        context = new CellularFiveLocalEntities();
    }

    private DbSet<TEntity> EntitySet
    {
        get { return context.Set<TEntity>(); }
    }

    public List<string> RawQuery(string query)
    {
        List<string> result = null;

        try {
            var entity = context.Set<string>();
            result = entity.SqlQuery(query).ToList();
        }
        catch (Exception ex) { 
            throw ex;
        }

        return result;
    }

    public void Dispose()
    {
        if (context != null)
        {
            context.Dispose();
        }
    }
}

如何使用通用对象进行自定义查询?

0 个答案:

没有答案
相关问题