![](/img/trans.png)
[英]ASP.NET Core - How to convert SQL Server statement to Entity Framework Core
[英]Execute sql statement in .net entity framework core
我创建了 ASP.NET 5.0 Core-Web-API 项目并使用 package Microsoft.EntityFrameworkCore
版本 5.0.13。 如果我运行简单的 sql 语句var simple = myDbContext.myTable.FromSqlInterpolated($"Select Id, colA, colB From Testvw")
一切都按预期工作。 如果我执行存储在文本文件中的相同查询,我会遇到错误:
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string sqlFile = Path.Combine(path, @"SQL_Ressourcen\myQuery.txt");
String sql = System.IO.File.ReadAllText(sqlFile);
var simple = myDbContext.myTable.FromSqlInterpolated($"{sql}");
System.InvalidOperationException
HResult=0x80131509
Nachricht = 'FromSqlRaw' or 'FromSqlInterpolated' was called with non-composable SQL and with a query composing over it. Consider calling 'AsEnumerable' after the method to perform the composition on the client side.
Quelle = Microsoft.EntityFrameworkCore.Relational
从文件加载查询字符串时如何执行查询?
我认为您可以执行以下操作:
var blogs = context.myTable
.FromSqlRaw($"Select Id, colA, colB From myTable")
.ToList();
// or AsEnumerable();
var blogs = context.myTable
.FromSqlRaw($"Select Id, colA, colB From myTable")
.AsEnumerable();
来源:原始 SQL 查询
否则,您可以尝试添加如下内容:
var simple = _context.myTable.FromSqlInterpolated($"Select Id, colA, colB From myTable").ToList();
问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.