本文介绍了如何使用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.
推荐答案您可以运行使用Entity Framework进行原始查询,例如:
using (var context = new BloggingContext()) { var blogNames = context.Database.SqlQuery<string>( "SELECT Name FROM dbo.Blogs").ToList(); }如果要返回更复杂的类型,则可以定义自己的类并使用它.只要属性与您选择的列的名称匹配,它将起作用.因此,让我们上一堂课:
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?
发布评论