【发布时间】:2021-06-21 04:53:20
【问题描述】:
我是第一次使用 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);
}
}
但我得到了错误
列“attachmentid”不存在。
所以我显然没有以正确的方式处理这件事。那么我需要做什么才能让它发挥作用呢?
【问题讨论】:
标签: c# postgresql dapper