I am using Dapper for the first time, I'm previously used to just writing my SQL directly.
I have a situation where I have a many-to-many relationship so that my classes look like this:
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;}
}
In my DB I have a table to link them together which looks like this;
ProductId
AttachmentId
(A composite primary key and both with Fk to their repective tables).
But I don't know how to perform the insert. What I have is below
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);
}
}
But I get the error
column "attachmentid" does not exist.
So I'm obviously not going about this in the right manner. So what do I need to do in order to get this to work?