如何使用Entity Framework 6.1返回dataReader?

编程入门 行业动态 更新时间:2024-10-26 00:21:30
本文介绍了如何使用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?

本文发布于:2023-05-31 10:04:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/389531.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   Entity   Framework   dataReader

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!