标签: npgsql

Parallel.For 与 Npgsql 一起使用吗?

是否可以将 Npgsql 函数与Parallel.For块一起使用?

这是我的示例,但出现异常“后端发送了无法识别的响应类型:\0”

    NpgsqlConnection PGconnexion = new NpgsqlConnection(...);
    string SqlParallel = "";
    int OID = -1;
    Parallel.For(0, 100, i =>
    {
        SqlParallel = string.Format(@"INSERT INTO ParallelTest(Id) VALUES({0}) RETURNING OID;", i);
        using (NpgsqlCommand PGcommandParallel = new NpgsqlCommand(SqlParallel, PGconnexion)) 
        {
            try { OID = (int)PGcommandParallel.ExecuteScalar(); }
            catch (Exception ex) { Console.WriteLine(ex.Message); }
        }
        Console.WriteLine("Insert: {0} OID: {1}", i, OID);
    });
Run Code Online (Sandbox Code Playgroud)

和 PostgreSql 表架构

-- Table: paralleltest

-- DROP TABLE paralleltest;

CREATE TABLE paralleltest
(
  id integer
)
WITH ( …
Run Code Online (Sandbox Code Playgroud)

c# postgresql parallel-processing npgsql

3
推荐指数
1
解决办法
3059
查看次数

实体框架中的Postgresql表继承

我想知道是否可以在实体框架 7 中进行 postgres 表继承 - 而不是添加列,而是使用基表和继承基表的表来执行“真正的”postgres?

如果是这样,我该如何配置?

干杯,马里奥

postgresql npgsql entity-framework-core

3
推荐指数
1
解决办法
1780
查看次数

使用 Dapper 和 Postgresql - citext 数据类型

我不确定是否有办法支持这一点,但我无法让 Dapper 将字符串参数值映射到 Postgresql citext 数据类型,因为它似乎正在使用文本类型。

特别是,我试图调用一个接受 citext 参数的函数 - 我得到的错误是:

var c = ConnectionManager<T>.Open();
string sql = @"select * from ""dbo"".""MyFunction""(@schemaName, @tableName);";
var param = new
{
    schemaName = schema,
    tableName = table
};

string insecureSalt = c.QueryMultiple(sql, param).Read<string>().FirstOrDefault();
ConnectionManager<T>.Close(c);

Error: Npgsql.PostgresException: 42883: function dbo.MyFunction(text, text) does not exist.
Run Code Online (Sandbox Code Playgroud)

匹配的签名是 function dbo.MyFunction(citext, citext) 很明显它无法使用默认映射找到它。

根据 Npgsql - http://www.npgsql.org/doc/types.html我需要能够将 NpgsqlDbType.Citext 指定为类型,但我找不到使用 Dapper 执行此操作的方法。

解决了由于这里离吉文回答,完整的解决方案:

var c = ConnectionManager<T>.Open();
string sql = @"select * from ""dbo"".""MyFunction""(@schemaName, @tableName);";
var param …
Run Code Online (Sandbox Code Playgroud)

postgresql npgsql dapper

3
推荐指数
1
解决办法
2714
查看次数

将 C# 批量复制到 PostgreSql

我有一个包含 1000 条记录的数据表。我想执行从 C# 到 PGSQL 的批量插入操作。我知道一种将文本文件复制到的方法pgtable.

示例语法如下:

using (NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=postgres;Password=postgres;Database=postgres;"))
{
    onn.Open();
    NpgsqlCommand command = new NpgsqlCommand("copy \"schema\".\"tablename\" (\"col1\",\"col2\") from 'C:\\datafile.txt'", conn);
    command.ExecuteNonQuery();
    conn.Close();
}
Run Code Online (Sandbox Code Playgroud)

是否有任何其他函数可以用来传递数据表而不是将数据写入文本文件?我在用着Npgsql.dll

c# postgresql npgsql

3
推荐指数
1
解决办法
2万
查看次数

PostgreSQL 11 - 过程支持

  • PostgreSql 版本:11 Beta 4
  • Npgsql 版本:v4.0.3

我想测试 PostgreSql 的 Store Prodecure,但出现错误。

"42809: get_customer_list() is a procedure"
Run Code Online (Sandbox Code Playgroud)

我的命令类型

CommandType.StoredProcedure
Run Code Online (Sandbox Code Playgroud)

Npgsql 是否支持 PostgreSql 11 Store Prodecure ?

postgresql npgsql

3
推荐指数
1
解决办法
504
查看次数

可以在 CodeFirst EntityFramework 中使用 NPGSQL 指定 JSONB GIN 索引吗?

我在 EFCore 项目中的 EF 代码优先对象上有一些 JSONB 列。我想为那些存储为 JSONB 属性/列的强类型对象内的属性设置一些 GIN 索引。
这可能吗?(目前使用 postgres:latest image)

谢谢。

postgresql entity-framework npgsql

3
推荐指数
1
解决办法
676
查看次数

EF 迁移后 NPGSQL MapEnum 失败

根据文档enum,我添加了一个实体框架并将其注册到 NPGSQL 中:

static DataContext()
{
    NpgsqlConnection.GlobalTypeMapper.MapEnum<MyEnum>();
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.ForNpgsqlHasEnum<MyEnum>();
    ...
}
Run Code Online (Sandbox Code Playgroud)

运行迁移并启动 Web 应用程序后,抛出以下异常:

CLR 枚举类型 MyEnum 必须在使用前向 Npgsql 注册,请参考文档

如果我再次重新启动应用程序,它工作得很好 - 这只会在MyEnum应用了添加的迁移后立即发生。

c# entity-framework npgsql entity-framework-core

3
推荐指数
1
解决办法
906
查看次数

EF Core In-Memmory Array mapping

I have a string array column in database

ALTER TABLE sample ADD languages VARCHAR[] NULL;
Run Code Online (Sandbox Code Playgroud)

which is mapped to model:

public string[] languages { get; set; }
Run Code Online (Sandbox Code Playgroud)

Using EF Core with PostgreSQL (Npgsql.EntityFrameworkCore.PostgreSQL 2.2.0) works out of the box.

However, when I want to test DbContext using In-Memmory (Microsoft.EntityFrameworkCore.InMemory 2.2.4) I got this error:

System.InvalidOperationException: 无法映射属性“sample.languages”,因为它的类型“string[]”不是受支持的基本类型或有效的实体类型。显式映射此属性,或使用 '[NotMapped]' 属性或使用 'OnModelCreating' 中的 'EntityTypeBuilder.Ignore' 忽略它。

因此,将其添加到模型构建器配置中可以解决 In-Memory 错误:

 builder.Property(p => p.languages)
                    .HasConversion(
                        v => string.Join("'", v),
                        v => v.Split(',', StringSplitOptions.RemoveEmptyEntries));
Run Code Online (Sandbox Code Playgroud)

但引发了新的(Npgsql):

无法在 Npgsql.NpgsqlDefaultDataReader.GetFieldValue[T](Int32 …

entity-framework npgsql ef-core-2.2

3
推荐指数
1
解决办法
1213
查看次数

Dapper 和 Postgres - 打印查询

我在 Postgres 中使用 Dapper,我想记录一些诊断信息,尤其是正在运行的实际查询。

如果我使用的是 SQL Server,我会这样做

let connection = new SqlConnection(connectionString)
connection.StatisticsEnabled = true

// run query 

let stats = connection.RetrieveStatistics()
Run Code Online (Sandbox Code Playgroud)

但我似乎找不到类似的东西NpgsqlConnection

检索正在运行的实际查询的最佳方法是NpgsqlConnection什么?

.net f# npgsql dapper

3
推荐指数
1
解决办法
390
查看次数

命令已经在进行中

我正在尝试为我正在开发的 Web 应用程序运行后台工作程序。我使用 Npgsql 作为我的 EF Core 提供程序。

为了澄清起见,我已经为我注入DbContext了一个瞬态生命周期,并在我的连接字符串中允许池化,但是,每当我尝试测试它时,我都会收到以下错误:

Npgsql.NpgsqlOperationInProgressException:命令已经在进行中:[My Query Here]

我的Program班级设置如下:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureServices((hostContext, services) =>
            {
                // Get the configuration
                IConfiguration config = hostContext.Configuration;

                // DbContext
                services.AddDbContext<MyDbContext>(options => options.UseNpgsql(config.GetConnectionString("PostgreSQLString")), ServiceLifetime.Transient);

                services.AddHostedService<Worker>();
                services.AddScoped<IDTOService, BackgroundDTOService>();
            });
}
Run Code Online (Sandbox Code Playgroud)

然后通向我的Worker班级

public class Worker : BackgroundService
{
    private Logger logger;

    public Worker(IServiceProvider services, IConfiguration configuration)
    {
        this.Services = services; …
Run Code Online (Sandbox Code Playgroud)

c# npgsql entity-framework-core

3
推荐指数
2
解决办法
9668
查看次数