简体   繁体   English

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

[英]How to insert data and return inserted identity with Dapper in MVC?

I have a model Products like this, 我有这样的模型产品,

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

Here is my view, 这是我的看法,

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

Here is my 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();
 }

And here is my sql code, 这是我的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

I am getting "procedure or function ProductCreate has too many arguments specified" error. 我得到“过程或函数ProductCreate指定了太多参数”错误。

What am I doing wrong? 我究竟做错了什么?

When you call a stored procedure, dapper has no good way of knowing what members should be treated as parameters. 当您调用存储过程时,dapper无法知道哪些成员应被视为参数。 With regular tsql, it can have a stab at "obviously not used", " obviously used", "maybe" - but dapper does not choose to pay the extra cost of asking the database what your stored procedure looks like all the time. 对于常规的tsql,它可能会“明显不使用”,“明显使用”,“可能” - 但是dapper并没有选择支付额外的费用来询问数据库您的存储过程一直是什么样子。 I suspect it is adding properties that aren't used - ProductId in particular. 我怀疑它正在添加未使用的属性 - 特别是ProductId Help it out by mapping to an anon type. 通过映射到anon类型来帮助它。 Instead of passing model , pass: 传递:而不是传递model

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

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

相关问题 如何使用 Dapper 执行插入和返回插入的标识? - How do I perform an insert and return inserted identity with Dapper? 如何从插入sql语句返回值,即是否插入了数据 - How return value from insert sql statment that data inserted or not SQLHelper Class - 插入/更新方法返回插入的 ID @@identity - SQLHelper Class - Insert/Update method return inserted ID @@identity 如何在与Entity Framework连接并使用dapper的asp.net mvc中插入和检索地理数据? - How do I insert and retrieve geography data in asp.net mvc connected with Entity Framework and using dapper? SQLITE-INSERT不返回错误,但未插入任何数据 - SQLITE - INSERT does not return error but no data is inserted 如何在dapper中返回原始数据类型的元组 - how to return tuple of primitive data types in dapper Dapper 使用存储过程返回插入的值 - Dapper return inserted value using stored procedures 如何在C#中使用一个命令将条目插入SQL Server数据库并返回包含主键标识的已插入列? - How do I insert an entry into a SQL Server database and return inserted columns including primary key identity with one command in C#? C# Web API 使用 Dapper 使用存储过程插入数据 - 无法获得插入的记录 ID - C# Web API using Dapper to insert data using a stored procedure - unable to get inserted record Id Dapper.SimpleCRUD插入没有身份的问题 - Dapper.SimpleCRUD Insert issue without identity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM