SqlException: 过程或函数 sqQuestion_AddNewQuestion 指定的参数太多

SqlException: Procedure or function sqQuestion_AddNewQuestion has too many arguments specified

我正在尝试将一些数据输入本地 SQL 服务器数据库。然而,尽管多次计算参数的数量,我仍然得到这个错误:

Procedure or function sqQuestion_AddNewQuestion has too many arguments specified

问题出现在SqlDataAccess.SaveDataInTransaction。关于此的许多其他问题都涉及导致问题的 foreach 循环,但我没有使用它。

我是 C# 的新手,所以我可能遗漏了一些显而易见的东西。

代码如下:

dbo.Questions:

CREATE TABLE [dbo].[Questions]
(
    [Id] INT NOT NULL PRIMARY KEY IDENTITY(1,1), 
    [Topic] NVARCHAR(50) NOT NULL, 
    [Title] NVARCHAR(50) NOT NULL, 
    [Description] NVARCHAR(200) NOT NULL, 
    [AuthorId] NVARCHAR(128) NOT NULL, 
    [Image] NVARCHAR(MAX) NULL, 
    [CreatedDate] DATETIME2 NOT NULL DEFAULT getutcdate()
)

存储过程:

CREATE PROCEDURE [dbo].[sqQuestion_AddNewQuestion]
    @Topic nvarchar(50),
    @Title nvarchar(50),
    @Description nvarchar(200),
    @AuthorId nvarchar(128),
    @CreatedDate datetime2,
    @Image nvarchar(Max)
AS
BEGIN
    SET NOCOUNT ON

    INSERT INTO dbo.Questions(Topic, Title, [Description], AuthorId, CreatedDate, [Image])
    VALUES (@Topic, @Title, @Description, @AuthorId, @CreatedDate, @Image)
END

问题数据库模型:

public class QuestionDbModel
{
    public int Id { get; set; }
    public string Topic { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Image { get; set; }
    public string AuthorId { get; set; }
    public DateTime CreatedDate { get; set; }
    public List<CommentModel> Comments { get; set; }
}

QuestionModel(用于允许用户上传图片):

public class QuestionModel
{
    public int Id { get; set; }
    public string Topic { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    [DisplayName("Image")]
    public string Image { get; set; }
    [NotMapped]
    [DisplayName("Upload Image")]
    public IFormFile ImageFile { get; set; }
    public string AuthorId { get; set; }
    public DateTime CreatedDate { get; set; }
    public List<CommentModel> Comments { get; set; }
}

仪表板控制器:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(QuestionModel question)
{
    var wwwRootPath = _hostEnvironment.WebRootPath;

    string file = Path.GetFileNameWithoutExtension(question.ImageFile.FileName);
    string ext = Path.GetExtension(question.ImageFile.FileName);
    string fileName = DateTime.Now.ToString("yymmssfff") + ext;

    var path = Path.Combine(wwwRootPath + "/QuestionImages/", fileName);

    using (var filestream = new FileStream(path, FileMode.Create))
    {
        question.ImageFile.CopyToAsync(filestream);
    }

    QuestionDbModel newQuestion = new QuestionDbModel
        {
            Topic = question.Topic,
            Title = question.Title,
            Description = question.Description,
            Image = fileName,
            AuthorId = question.AuthorId,
            CreatedDate = question.CreatedDate
        };

    try
    {
        _sqlDataAccess.StartTransaction("*DAtabaseName*");

        _sqlDataAccess.SaveDataInTransaction("dbo.sqQuestion_AddNewQuestion", newQuestion);

        _sqlDataAccess.CommitTransaction();

        return RedirectToAction(nameof(Index));
    }
    catch
    {
        _sqlDataAccess.RollbackTransation();
        throw;
    }
}

SqlDataAccess(SaveDataInTransaction):

public void SaveDataInTransaction<T>(string storedProcedure, T parameters)
{
    _connection.Execute(storedProcedure, parameters,
                        commandType: CommandType.StoredProcedure, transaction: _transaction);
}

class“QuestionDbModel”与预期参数列表不匹配。作为测试,尝试创建另一个 class,它的属性与存储过程期望的参数列表相匹配。如果这解决了问题,那么您可以决定使用继承或由“QuestionDbModel”class 支持的接口,但只公开 6 properties/parameters.