Dapper 如何使用相同的外键插入多行

问题描述 投票:0回答:0

我有一个子表,它引用具有一对多关系的父表。 PK -> 多个外键。原因是 FK 表是与父表上的 ID 相关的对象列表。

如何使用 dapper 插入具有相同 FK 的多行?

assessmentModel.locationInformationModels = new List<LocationInformationModel>();

string sqlStatement = @"INSERT INTO LocationInformation
                                ([LocationInformationId]                          
                                ,[Address1]
                                ,[Address2]
                                ,[City]
                                ,[State]
                                ,[Zip])
                                VALUES(
                                @LocationInformationId,@Address1,@Address2,@City,@State,@Zip)";            
         
using (var connection = new SqlConnection(_connectionString))
            {
                await connection.OpenAsync();
                using (var ts = connection.BeginTransaction())
                {
                    var retVal = await connection.ExecuteAsync(sqlStatement, assessmentModel.locationInformationModels, transaction:ts);
                    ts.Commit();
                    return retVal;
                }        
            }
c# sql-server dapper
© www.soinside.com 2019 - 2024. All rights reserved.