c# - Entity Framework Core 3 - 具有来自存储过程的一些字段的模型
问题描述
在 EF Core 3 中,我有一个模型,其中仅从旧存储过程中检索模型的几个字段。存储过程内置了大约 300 行业务逻辑,因此由于测试工作,我们并不热衷于将逻辑移出存储过程。
在这里执行 get 中的存储过程似乎不是正确的方法(它会导致InvalidOperation
异常)。将来自存储过程的列链接到现有模型的正确方法是什么?
我想到的唯一其他方法可能是在页面的加载属性中填写这些字段;但也许我错过了另一种更好的方式。:)
// This is my existing display model
internal partial class MyExistingModel
{
[Key]
public int id{ get; set; }
public int R_Id { get; set; }
public string G_Number { get; set; }
public virtual P1 ParentRuns { get; set; }
public virtual ICollection<P1> AnotherOne { get; set; }
}
这是为每个调用的存储过程标头MyExistingModel.id
:
get_ComplicatedLogicStuff(@in_id)
AS
SELECT
column1, column2, column3, column4
...
WHERE
table1.id = @in_id;
谢谢!
解决方案
我不会将该逻辑放在页面中,而是创建一个 ModelService 类来为您加载模型。然后您可以具体选择如何加载您的导航属性。这是您可以使用的一种方法。
internal class MyExistingModelService
{
public IEnumerable<MyExistingModel> List ()
{
using ( var context = new YourDataContextHere() ) {
return context.MyExistingData
.Select ( myExistingData => new MyExistingModel () {
Id = myExistingData.Id,
R_Id = myExistingData.R_Id,
AnotherOne = context.YourStoredProcedure().Where( (entity) => SomeFilter)
});
}
}
}
推荐阅读
- chromium - 如何找到 Chromium 内部版本号?
- java - 如何使用 Spring Cloud Function 将函数映射到 REST 兼容的端点
- haskell - 为什么这个 Haskell 函数中没有一个普遍量化的变量?
- c# - ASP.NET Core React 项目模板 - 如何链接到 C# 后端
- python - Ctypes:OSError:异常:堆栈溢出
- python - 在唯一元素的结构化 Numpy 数组中查找相邻值的最简单方法是什么?
- ionic-framework - 如何将 Phaser 3 集成到 Ionic 4?
- sql-server - 如何选择 SQL Server 表作为制表符分隔的字符串
- javascript - 当实际的鼠标点击没有在目标元素上开始时,如何防止触发点击事件?
- flutter - 如何在 Dart 中获取某物的类型?