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

标签 c# sql asp.net-mvc dapper

我有一个像这样的产品模型,

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>
}

这是我的 Controller ,

[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 指定的参数太多”错误。

我做错了什么?

最佳答案

当您调用存储过程时,dapper 没有很好的方法知道哪些成员应该被视为参数。对于常规的 tsql,它可以尝试“显然没有使用”、“显然使用过”、“可能”——但是 dapper 不会选择支付额外的费用来询问数据库你的存储过程是什么看起来一直都是。我怀疑它正在添加未使用的属性 - 特别是 ProductId。通过映射到 anon 类型来帮助它。不要传递 model,而是传递:

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

关于c# - 如何在 MVC 中使用 Dapper 插入数据并返回插入的标识?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33766148/

相关文章:

c# - 检查 HTTP 缓存上的空引用

php - 将多个true false单选按钮插入mysql

sql - 当您执行 `SELECT *` 时,SQL Server 如何确定列的顺序?

c# - 带有 ReportViewerWebControl 的 WebAPI Cookie

asp.net - CSS,Javascript和图片的长度为零

c# - 在 C# 中使用动态访问匿名类型的字段 - 可能吗?

c# - 尝试通过我的本地主机作为后端和 c# 作为前端为我的 MySQL 数据库创建项目

c# - 在 ASP.NET MVC 中使用 Redis 和 SQL 数据库有哪些可能性

mysql - 如何对两个 SQL 表中的所有值进行乘法和求和

asp.net-mvc - 如何通过我的 ASP.NET MVC 网站提供服务器日志文件?