.Net 核心控制台应用程序(EF 核心 2.0/Sql 服务器)中的以下代码。
var sql = _context.Set<string>()
.FromSql("select dbo.FunctionReturnVarchar({0});", id);
出现以下异常?
Cannot create a DbSet for 'string' because this type is not included in the model for the context.
是否可以不用定义字符串属性的类?
最佳答案
在这种情况下,您需要的是 ExecuteSqlCommand
具有绑定(bind)输出参数的方法:
var resultParameter = new SqlParameter("@result", SqlDbType.VarChar);
resultParameter.Size = 2000; // some meaningfull value
resultParameter.Direction = ParameterDirection.Output;
_context.Database.ExecuteSqlCommand("set @result = FunctionReturnVarchar({0});", id, resultParameter);
var result = resultParameter.Value as string;
关于c# - 如何从 .Net 核心应用程序中的 SQL 语句获取标量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46163254/