简体   繁体   中英

Insert multiple objects with dapper

I must add a list of objects using Dapper.

Dapper dynamic parameters throw a SQLException “must define scalar variable” is the error I receive after the first object is created with success in the database.

This is the cs :

     public class Inventorytransaction
     {
    //InventoryHeader
    public int id { get; set; }
    public int transactionumber { get; set; }
    public string storeroom { get; set; }
    public string transactiontype { get; set; }
    public string projectname { get; set; }
    public string comments { get; set; }
    public DateTime date { get; set; }

    //InventoryLine
    public string locationtype { get; set; }
    public string lc { get; set; }
    public string itemnum { get; set; }
    public decimal quantity { get; set; }
    public string uom { get; set; }
    public decimal unitprice { get; set; }

Here is the code that calls the procedure:

    public void AddInventoryTransaction(Inventorytransaction inventorytransaction)
    {    
        {
            List<Inventorytransaction> inventorytransactions = new List<Inventorytransaction>();
            inventorytransactions.Add(inventorytransaction);
            using (IDbConnection connection = new SqlConnection(_connectionString))

                {
                //TODO

                connection.Execute("dbo.spAddNewInventoryHeader @Storeroom, @Transactiontype,@Projectname, @Comments", inventorytransaction);

            }
        }

    }
    public void AddInventoryTransactionLine(Inventorytransaction inventorytransaction)
    {
            List<Inventorytransaction> inventorytransactions = new List<Inventorytransaction>();
        inventorytransactions.Add(inventorytransaction);
            using (IDbConnection connection = new SqlConnection(_connectionString))

            {
                //TODO

                connection.Execute("dbo.spAddNewInventoryLine  @Storeroom, @Locationtype,@Lc, @Itemnum,@Quantity,@Uom,@Unitprice", inventorytransaction);

        }

}

And this is the interface:

interface IInventorytransactionRepository
{
    void List<AddInventoryTransaction>();
}

Does anyone know what I'm doing wrong? With this method I'm able to create one insert in the database but no more.

You could potentially do the following, if your code warrants this level of abstraction. The object you have may not be as expressive as you like, Dapper supports complex object capabilities. You could do the following:

public class Invoice
{
     public int Id { get; set; }
     public int Transaction { get; set; }
     ...
     public IEnumerable<LineItem> LineItems { get; set; }
}

public class LineItem
{
     public int Id { get; set; }
     ...
}

Your invoice object will contain a line item, in Dapper you can do the following:

dbConnection.Execute<Invoice, LineItem, Invoice>("query", (invoice, lineItem) =>
{
     invoice.LineItem = lineItem;
     return invoice;
}, new { Id = "Parameters here" });

Dapper can do up to seven items, but this will allow a more comprehensive domain object in the code and still maintain your structure within the database. You can also do the above syntax and pass a collection of invoices with the complex object.

Not exactly what you asked, but I do feel it will help your application in the long run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM