如何使用Dapper在MVC中插入数据并返回插入的标识?

3

我有一个类似这样的产品模型:

public int ProductID { get; set; }
public int ProductDetailsID { get; set; }
public int ProductStatusID { get; set; }
public string ProductName { get; set; }
public int Priority { get; set; }
public DateTime LastProcessedDate { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime ModifiedDate { get; set; }
public bool Enabled { get; set; }

这是我的观点:
@model MVCApplication1.Models.Products
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
<div class="form-horizontal">
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(i => i.ProductDetailsID, new { @Value = ViewBag.ProductDetailsID })
    <div class="form-group">
        @Html.LabelFor(i => i.Name, "Product Name ", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(i => i.Name, new { @class = "form-control" })
            @Html.ValidationMessageFor(i => i.Name, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(i => i.Enabled, "Enabled ", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.CheckBoxFor(i => i.Enabled, new { @class = "checkbox", @checked = "checked" })
            @Html.ValidationMessageFor(i => i.Enabled)
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

这是我的控制器,
[HttpPost]
public ActionResult Create(Products model)
{
   try
   {
       if (ModelState.IsValid)
       {
           model.LastProcessedDate = DateTime.Now;
           model.CreatedDate = DateTime.Now;
           model.ModifiedDate = DateTime.Now;
           model.ProductStatusID = 0;
           int id = con.Query<int>("ProductInsert", model, commandType: CommandType.StoredProcedure).Single();

           return Content("Product added successfully..!!", "text/plain");
            }
        }
        catch (Exception)
        {
            throw;
        }
        return View();
 }

以下是我的SQL代码:

CREATE PROCEDURE [dbo].[ProductCreate]
(
  @ProductDetailsId INTEGER,
  @ProductStatusID INTEGER,
  @ProductName VARCHAR(255),
  @Priority INTEGER,
  @LastProcessedDate DATETIME,
  @CreatedDate DATETIME,
  @ModifiedDate DATETIME,
  @Enabled BIT
)
AS
INSERT INTO dbo.Products (ProductDetailsId, ProductStatusId, Product, Priority, LastProcessedDate, CreatedDate, ModifiedDate, Enabled, ScheduleTypeId, Server, RetryNumber, Message) 
VALUES(@ProductDetailsId, @ProductStatusId, @ProductName, @Priority, @LastProcessedDate, @CreatedDate, @ModifiedDate, @Enabled)

SELECT SCOPE_IDENTITY() AS ProductID

我遇到了“过多指定了参数的过程或函数ProductCreate”错误。

我做错了什么?


运行 SQL Profiler 并查看哪些参数未被传递。 - Steven Ackley
1
看起来你可能也传入了“ProductID”。 - JamieD77
您的模型有9个属性,但是期望只有8个参数。猜猜哪一个是闯入者? - Steve
ProductId 是入侵者吗? - Arpita
1个回答

0

当您调用存储过程时,Dapper并不知道哪些成员应该被视为参数。在常规的T-SQL中,它可以尝试“显然未使用”、“显然已使用”、“可能使用” - 但是Dapper并没有选择支付额外的费用来询问数据库一直是什么样子的存储过程。我怀疑它正在添加未使用的属性 - 特别是ProductId。通过映射到匿名类型来帮助它。而不是传递model,请传递:

new { model.ProductDetailsId, model.ProductStatusID, ...todo... ,
    model.Enabled }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接