如何使用Entity Framework 6.1返回dataReader? [英] How can i return a dataReader using Entity Framework 6.1?

查看:124
本文介绍了如何使用Entity Framework 6.1返回dataReader?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完全按照问题的要求.我的大多数代码都使用实体框架,但是我还需要从不在实体框架上下文内的sql表执行并返回计数或列.

Exactly as the question asks. I am using entity framework for most of my code, but i also need to execute and return a count or columns from a sql table that is not within my entity framework context.

推荐答案

您可以运行

如果要返回更复杂的类型,则可以定义自己的类并使用它.只要属性与您选择的列的名称匹配,它将起作用.因此,让我们上一堂课:

If you want to return a more complex type, you can define your own class and use that instead. As long as the properties match the names of the columns you select, it will work. So lets make a class:

public class MyClass
{
    public int Id { get; set; }
    public string UserName { get; set; }
}

并使用它:

List<MyClass> result = ctx
    .Database.SqlQuery<MyClass>("SELECT Id, UserName FROM dbo.Users")
    .ToList();

这篇关于如何使用Entity Framework 6.1返回dataReader?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆