我想使用 LINQ 和 EntityFramework 上下文定义数据库查询,但我不希望返回实体;我想要一个数据读取器!
我怎样才能做到这一点?这是为了将行导出到 CSV。
干杯,伊恩。
如果你需要这个,你很可能正在做一些意想不到的事情。通过查询的具体化结果进行简单迭代应该就是您所需要的 - 这就是 ORM 方式。如果你不喜欢它使用SqlCommand
直接地。
DbContext API 已被简化,因此它不包含 ObjectContext API 中可用的许多功能。访问数据读取器就是其中之一。您可以尝试转换DbContext
to 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))
{
...
}
}
但纯 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())
{
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)