簡體   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