【问题标题】:EF Core call stored procedure and get list of entityEF Core 调用存储过程并获取实体列表
【发布时间】:2021-01-04 03:38:04
【问题描述】:

我将 EF Core 和 SQL Server 本地用于我的 ASP.NET Core 后端。在 SQL Server 中,我有一个带有多个连接的长而复杂的参数化存储过程。

我这样称呼它:

[HttpGet("plantpl/{plantId}&{year}&{costAccTypeId}")]
public IActionResult GetPlantPl(long plantId, int year, long costAccTypeId)
{
    SqlParameter plantIdParam = new SqlParameter("@PlantId", plantId);
    SqlParameter yearParam = new SqlParameter("@Year", year);
    SqlParameter costAccTypeIdParam = new SqlParameter("@CostAccTypeId", costAccTypeId);
    
    var plantPLRows = DataContext.PlantPLRows.FromSqlRaw("PlantPl @PlantId, @Year, @CostAccTypeId",
            plantIdParam, yearParam, costAccTypeIdParam);

    return Json(plantPLRows);
}

为了使用FromSqlRaw方法,我必须创建一个模型类PlantPLRow和对应的DbSet PlantPLRows。

基本上,我只是想从数据库中取回一些数据并将其发送给客户端。我不想要一个新的DbSet 也不想要迁移。但是要使用FromSqlRaw,我必须使用它。

所以我的问题是:这是否适合我的情况?

【问题讨论】:

  • 是的,这就是使用 EF Core 的方式

标签: asp.net-core entity-framework-core


【解决方案1】:

@Sergey,API 调用,它的签名/属性和控制器代码都可以,无需更改。

@ErikEj 的This 博客文章给出了提示。我不得不将这行代码添加到我的DataContext 类中:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<PlantPLRow>().HasNoKey();
}

【讨论】:

    猜你喜欢
    • 2019-12-08
    • 1970-01-01
    • 2023-02-11
    • 2020-01-04
    • 2022-11-27
    • 2021-05-14
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    相关资源
    最近更新 更多