Ian*_*ton 15 ado.net entity-framework entity-framework-4.1
我想使用LINQ和我的EntityFramework上下文定义数据库查询,但我不希望返回实体; 我想要一个datareader!
我怎样才能做到这一点?这用于将行导出为CSV.
干杯,伊恩.
Lad*_*nka 19
如果你需要这个,你更可能做出意想不到的事情.通过查询的具体化结果进行简单迭代应该是您所需要的 - 这就是ORM方式.如果你不喜欢它SqlCommand直接使用.
简化了DbContext API,因此它不包含ObjectContext API中提供的许多功能.访问数据阅读器就是其中之一.你可以尝试转换DbContext到ObjectContext并使用更复杂的API:
ObjectContext objContext = ((IObjectContextAdapter)dbContext).ObjectContext;
using (var connection = objContext.Connection as EntityConnection)
{
// Create Entity SQL command querying conceptual model hidden behind your code-first mapping
EntityCommand command = connection.CreateCommand();
command.CommandText = "SELECT VALUE entity FROM ContextName.DbSetName AS entity";
connection.Open();
using (EntityDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess))
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
但纯ADO.NET方式更容易,更快,因为前一个例子仍然使用查询到SQL查询的映射:
using (var connection = new SqlConnection(Database.Connection.ConnectionString))
{
SqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM DbSetName";
connection.Open();
using(SqlDataReader reader = command.ExecuteReader())
{
}
}
Run Code Online (Sandbox Code Playgroud)
这个问题是围绕EF 4,但对于EF 6或更高版本的其他人,您可以使用AsStreaming()扩展方法.
http://msdn.microsoft.com/en-us/library/dn237204(v=vs.113).aspx
| 归档时间: |
|
| 查看次数: |
24275 次 |
| 最近记录: |