繁体   English   中英

无法首先从数据库中使用EF6代码调用“选择”存储过程

[英]Unable to call “select” stored procedure using EF6 code first from database

此代码失败,并显示错误:“必须声明标量值@prodID”。 有什么建议么?

using (var ctx = new StewieDataModel())
    {
        string productID = "81";

        var techData = ctx.Database.SqlQuery<TechData>("dbo.usp_DS_GetTechData @prodID", productID).ToList();
     }

这是模型:

namespace DatasheetData
{
    using System;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;

    public partial class StewieDataModel : DbContext
{
    public StewieDataModel()
        : base("name=StewieConnectionString")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    }
}

}

这是我要填充的课程:

namespace DatasheetData
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

[Table("usp_DS_GetTechData")]
public partial class TechData
    {

        public string TestCategory { get; set; }

        public string TestName { get; set; }

        public string TestResultLabel { get; set; }

        public string TestResult { get; set; }

        public string TestMethod { get; set; }
    }
}

这是我在SSMS中成功调用的方式:

DECLARE @return_value int

EXEC    @return_value = [dbo].[usp_DS_GetTechData]
        @prodID = "81"

SELECT  'Return Value' = @return_value

GO

SSMS结果是VarChar数据的四列: 在此处输入图片说明

您需要将参数作为SqlParameter对象传递。 这样的事情应该起作用:

var techData = ctx.Database.SqlQuery<TechData>(
        "dbo.usp_DS_GetTechData @prodID", 
        new SqlParameter("prodID", productID)
    ).ToList();

DavidG正确答案的一种简写替代形式是:

var techData = ctx.Database.SqlQuery<TechData>(
              "dbo.usp_DS_GetTechData @prodID = {0}", productID).ToList();

这里解释

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM