【问题标题】:ASP.NET Core and EF Core 1.1 - Display data using Stored ProcedureASP.NET Core 和 EF Core 1.1 - 使用存储过程显示数据
【发布时间】:2019-12-13 17:41:05
【问题描述】:

我的查询有问题。我想在视图上显示结果。

[HttpGet]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Index()
{
    return View(await _Context.Employee
                              .FromSql("EXEC sp_GetLoanDetails")
                              .ToArrayAsync());
}

这是我要查看的项目列表:

public class StoredProcRow
{
    [Key]
    public int empID { get; set; }
    public string empFullName { get; set; }
    public double EducationalLoan { get; set; }
    public double PettyCash { get; set; }
    public double BusinessLoan { get; set; }
    public double ApplianceLoan { get; set; }
    public double EmergencyLoan { get; set; }
    public double AllPurposeLoan { get; set; }
    public double KAPUSOIILoan { get; set; }
    public double FiestaLoan { get; set; }
    public double SalaryLoan { get; set; }
    public double Pledge { get; set; }
    public double PagIbigLoan { get; set; }
    public double SSSLoan { get; set; }
    public double AllAroundLoan { get; set; }
    public double Total { get; set; }
}

注意:这些实体名称与sp_GetLoanDetails中列名称上的实体相同

现在可以在 EF Core 1.1 上实现吗?还是我需要返回手动 ADO.NET 代码?

谢谢!

【问题讨论】:

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


【解决方案1】:

虽然 Entity Framework Core yet 不完全支持存储过程,但您仍然可以使用 FromSql 来使用存储过程。

为了做到这一点,数据库上下文需要知道您从存储过程映射到的实体。不幸的是,现在唯一的方法是将其实际定义为数据库上下文中的实体:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<StoredProcRow>(entity =>
    {
        // …
    });
}

然后,您可以通过在该实体的集合上运行 FromSql 方法来使用存储过程:

public virtual IQueryable<StoredProcRow> GetLoanDetails()
{
    return Set<StoredProcRow>().FromSql("[sp_GetLoanDetails]").AsNoTracking();
}

请注意,我在这里使用AsNoTracking 来避免数据上下文来跟踪来自存储过程的实体的更改(因为无论如何您都无法更新它们)。此外,我在方法中使用了Set&lt;T&gt;(),以避免必须将类型公开为数据库上下文中的成员,因为无论如何您都无法在没有存储过程的情况下使用集合。

顺便说一句。您在传递给FromSql 的sql 语句中不需要(不确定这是否有效)EXEC。只需将存储过程名称和任何参数传递给它,例如:

Set<MyEntity>().FromSql("[SomeStoredProcedure]");
Set<MyEntity>().FromSql("[SProcWithOneArgument] @Arg = {0}");
Set<MyEntity>().FromSql("[SProcWithTwoArguments] @Arg1 = {0}, Arg2 = {1}");

【讨论】:

  • 嗨@poke,我对Set的东西有点问题,我需要下载Microsoft.EntityFrameworkCore.Relational吗?只是一个想法,因为Set&lt;MyEntity&gt;.FromSqlthingy 出了点问题......
  • 是的,为了运行FromSql,你需要依赖Microsoft.EntityFrameworkCore.Relational,因为SQL是一个关系型的东西。
  • 感谢您的信息。它现在解决了我的问题...感谢您的解决方案..节省了我的时间。
  • @poke 假设我的 sp 正在返回一个包含 3 列的结果集,例如 RName、RId、OrgId。我用这三个属性创建了一个实体。像下面的 public class resultcollection { public int Rid{ get;放; } 公共 Nullable OrgId{ 获取;放; } 公共字符串 RName{ 得到;放;如果我需要调用 sp,那么在 ModelCreating 上应该是什么样子。
  • @user1447718 您可以像任何其他实体一样为存储过程设置实体(请参阅docs 以获得帮助)。所以你甚至可以使用数据注释,然后你只需要一个电话modelBuilder.Entity&lt;ResultCollection&gt;()。或者您使用实体构建器显式设置模型。
【解决方案2】:

从 NuGet 添加 System.Data.Common 和 System.Data.SqlClient。这些允许运行 ADO.NET 命令,即存储过程。

https://forums.asp.net/post/6061777.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多