精巧的多对多插入器 [英] Dapper many-to-many insert

查看:0
本文介绍了精巧的多对多插入器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次使用Dapper,我以前习惯于直接编写我的SQL。

我有一种情况,我有一个多对多的关系,因此我的类如下所示:

public class Product
{
    public int Id {get;set;}
    public string otherpropertiesremovedforbrevity Other {get;set;}
    ...
    public List<Attachment> Attachment {get;set;}
}


public class Attachment
{
    public int Id {get;set;}
    public string Name {get;set;}
    public string URL {get;set;}
}

在我的数据库中,我有一个将它们链接在一起的表,如下所示;

ProductId 
AttachmentId

(一个复合主键和两个主键,并将FK指向它们的检索表)。

但我不知道如何执行插入。我所拥有的内容如下

  using (var connection = GetConnection)
  {
      connection.Open(); 
      using (var transaction = connection.BeginTransaction())
      {
          string pINSERT = "INSERT INTO prodcat.Product (otherpropertiesremovedforbrevity) " +
                " VALUES (@otherpropertiesremovedforbrevity) RETURNING Id;";

          string sql = "insert into prodcat.AttachmentProductSpecificationLink (ProductSpecificationId, AttachmentId) values(@Id, @AttachmentId)";


          var res = await connection.ExecuteScalarAsync(pINSERT, entity);    
          var arows = await connection.ExecuteAsync(aINSERT, entity.Attachment, transaction);

          transaction.Commit();
          return Convert.ToInt64(res);

      }
  }

但我收到错误

列";附件不存在。

所以我显然没有以正确的方式来做这件事。那么,我需要做什么才能使其正常工作?

推荐答案

我相信下面的代码片段将为您工作。

您应该有3个INSERT语句,分别用于产品、附件和AttachmentProductSpecificationLink。在您的示例代码中,您遗漏了一个。我假设您的aINSERT应该用于附件表。

我注意到的另一件事是,ExecuteScalarAsync返回的是一个对象,因此我使用ExecuteScalarAsync<;int&>进行正确的强制转换。

您的实体(Product类)变量包含一个附件列表,通常我会执行一个ExecuteAsync并传入该列表,因为它是不带返回子句的普通插入。但在本例中,您返回的是ID,所以我选择使用Foreach循环来迭代插入的附件。

在Foreach循环中,我将返回的ID返回到变量attachmentID中,并将其用于以下对AttachmentProductSpecificationLink的插入。由于我们没有返回任何内容,因此我执行了ExecuteAsync。

using (var connection = GetConnection())
            {
                connection.Open();
                using (var transaction = connection.BeginTransaction())
                {
                    string pINSERT = "INSERT INTO prodcat.Product (otherpropertiesremovedforbrevity) " +
                          " VALUES (@otherpropertiesremovedforbrevity) RETURNING Id;";

                    string aINSERT = "INSERT INTO prodcat.Attachment (Name, URL) " +
                          " VALUES (@Name, @Url) RETURNING Id;";

                    string sql = "insert into prodcat.AttachmentProductSpecificationLink (ProductSpecificationId, AttachmentId) values(@Id, @AttachmentId)";


                    var res = await connection.ExecuteScalarAsync<int>(pINSERT, entity, transaction);

                    foreach( var a in entity.Attachment)
                    {
                        var attachmentId = await connection.ExecuteScalarAsync<int>(aINSERT, a, transaction);

                        var arows = await connection.ExecuteAsync(sql, new { Id = res, AttachmentId = attachmentId }, transaction);
                    }

                    transaction.Commit();
                    return Convert.ToInt64(res);

                }
            }

这篇关于精巧的多对多插入器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆