EF核心3中的非查询存储过程

时间:2020-04-01 09:48:06

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

我是ASP.net核心的新手,我想创建一个MVC网站,该网站显示学生数据,并在用户单击更改状态按钮时将其状态从校外更改为新。下面是类,数据库表和SP。

控制器中的更改状态方法:

public async Task<IActionResult> ChangeStatus(long? id)
    {
         if (id == null)
        {
            return NotFound();
        }

        var students = await _db.Students
            .FirstOrDefaultAsync(m => m.Id == id);
        if (students == null)
        {
            return NotFound();
        }
        var Id = new SqlParameter
        {
            ParameterName = "@Id",
            SqlDbType = System.Data.SqlDbType.BigInt,
            Direction = System.Data.ParameterDirection.Input,
            Value = student.Id
        };

        _db.Students.FromSqlInterpolated($" EXECUTE [dbo].[usp_changeStatus] @Id = {Id}").AsEnumerable().FirstOrDefault();
        await _db.SaveChangesAsync();
        return View(student);
    }

Model类:

 public class Student
    {
            [Key]
            public Int64 Id { get; set; }
            public string Name { get; set; }
            public string Address { get; set; }
            public string Age { get; set; }
    }

DBContext:

public DbSet<Student> Students{ get; set; }

在数据库中:

表格:

   CREATE TABLE [dbo].[Students](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](100) NOT NULL,
    [Address] [nvarchar](100) NOT NULL,
    [Age] [nvarchar](100) NOT NULL);

StoredProcedure:

  Create PROCEDURE [dbo].[usp_changeStatus]
    @Id bigint
AS
BEGIN
    SET NOCOUNT ON;
    update [dbo].[Students] set [Status] = 'New'
    where Id = @Id;
END

问题: 单击更改状态按钮后,SP运行,并且状态在数据库表中已更改,但是出现下图中的错误:

enter image description here

0 个答案:

没有答案