使用 .NET 核心中的 dapper 批量插入 PostgreSQL

Mar*_*her 5 c# postgresql dapper .net-core asp.net-core

我是使用 Dapper 的新手,我只是不知道如何将 json 文件中的属性列表插入到我的 postgres 数据库中。

所以我们开始我的对象类:

public class UniqueIdField
{
    public string name { get; set; }
    public bool isSystemMaintained { get; set; }
}

public class SpatialReference
{
    public int wkid { get; set; }
    public int latestWkid { get; set; }
}

public class Field
{
    public string name { get; set; }
    public string type { get; set; }
    public string alias { get; set; }
    public string sqlType { get; set; }
    public int length { get; set; }
    public object domain { get; set; }
    public object defaultValue { get; set; }
}

public class Attributes
{
    public string GEN { get; set; }
    public string BL { get; set; }
    public string BL_ID { get; set; }
    public string county { get; set; }
    public string last_update { get; set; }
    public double cases7_per_100k { get; set; }
    public double cases7_bl_per_100k { get; set; }
    public string cases7_per_100k_txt { get; set; }
    public string BEZ { get; set; }
}

public class Feature
{
    public Attributes attributes { get; set; }
}

public class Root
{
    public string objectIdFieldName { get; set; }
    public UniqueIdField uniqueIdField { get; set; }
    public string globalIdFieldName { get; set; }
    public string geometryType { get; set; }
    public SpatialReference spatialReference { get; set; }
    public List<Field> fields { get; set; }
    public List<Feature> features { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是我插入数据的方法:

    public async Task SeedCoronaInformationTable(Root CoronaData)
    {
        var constr = _configuration.GetConnectionString("PostgresConnection");

        using var connection = new NpgsqlConnection(constr);
        connection.Open();

        var deleteStatement = @"DELETE FROM public.rki_corona_information";
        var insertStatement = @"INSERT INTO public.rki_corona_information (id, landkreis, bl, bl_id, county, last_update, cases7_per_100k, cases7_bl_per_100k, cases7_per_100k_txt, BEZ)
                               Values (@id, @landkreis, @bl, @bl_id, @county, @last_update, @cases7_per_100k, @cases7_bl_per_100k, @cases7_per_100k_txt, @BEZ)";

        connection.Query(deleteStatement);

        int index = 1;
        foreach (var feature in CoronaData.features)
        {
            var affectedRows = await connection.ExecuteAsync(insertStatement, new
            {
                id = index,
                landkreis = feature.attributes.GEN,
                bl = feature.attributes.BL,
                bl_id = feature.attributes.BL_ID,
                feature.attributes.county,
                feature.attributes.last_update,
                feature.attributes.cases7_per_100k,
                feature.attributes.cases7_bl_per_100k,
                feature.attributes.cases7_per_100k_txt,
                feature.attributes.BEZ
            }); ;
            index++;
        }

        connection.Close();
    }
Run Code Online (Sandbox Code Playgroud)

后者有效,但是,我认为我必须自己遍历对象是完全没有必要的。我宁愿插入它并让 dapper 处理循环。我只是找不到如何传递属性列表的方法,因为它是features.

如何获取属性并插入它们var affectedRows = await connection.ExecuteAsync(insertStatement, attributes)

Mar*_*ell 3

没有支持的语法来执行此操作。您所拥有的应该可以正常工作 - 请注意,删除步骤可以/应该使用执行而不是查询。