【发布时间】:2020-10-29 13:37:09
【问题描述】:
我有一个存储过程,它获取多个参数并基于它们调用另一个存储过程。 返回的列可能会根据实际调用的存储过程而有所不同。
所以我构建了一个包含所有共享列的基类,并使用不同的类对其进行扩展,每个类都有自己的附加列作为要从存储过程返回的数据的模型。
以下是类示例:
public class SP_BaseModel
{
public decimal? Sum { get; set; }
public string Name { get; set; }
public DateTime? BirthDate { get; set; }
}
public class SP_Extended1 : SP_BaseModel
{
public int? Age { get; set; }
public string Hobby { get; set; }
}
我正在像这样在 DbContext 中注册上述模型:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SP_BaseModel>().HasNoKey();
modelBuilder.Entity<SP_Extended1>();
...
}
我尝试了以下两种方法,但都不起作用:
public IQueryable<T> GetData<T>(string param1, int? patam2 = null, DateTime? param3 = null) where T : SP_BaseModel
{
return Context.Set<T>()
.FromSqlInterpolated($"EXEC SP_GetData {param1},{param2 ?? null},{param3 ?? null}");
}
public async Task<List<T>> GetData<T>(string param1, int? patam2 = null, DateTime? param3 = null) where T : SP_BaseModel
{
return Context.Set<T>()
.FromSqlInterpolated($"EXEC SP_GetData {param1},{param2 ?? null},{param3 ?? null}").ToListAsync();
}
我正在从中创建一个 Nuget,并在客户端中使用它:
public List<SP_Extended1> GetData(DateTime? today = null)
{
today ??= DateTime.Today;
//This didn't work
var list1 = unitOfWork.MyRepository.GetData<SP_Extended1>(...);
//This didn't work as well
var list2 = unitOfWork.GetData<SP_Extended1>(...).AsEnumerable();
return ...
}
但我收到此错误消息:
'FromSqlRaw or FromSqlInterpolated was called with non-composable SQL and with a query composing over it.
Consider calling `AsEnumerable` after the FromSqlRaw or FromSqlInterpolated method to perform the composition on the client-side.'
【问题讨论】:
-
SP_GetData是表值函数吗? (为什么 EF Core 用户不指定数据库引擎) -
什么意思?
标签: c# .net-core entity-framework-core