标签: npgsql

有没有办法在运行时动态加载数据提供程序?

我不想修改我的machine.config,但我想使用Npgsql库作为数据提供程序.我将我的应用程序部署到多个平台(包括单声道),所以我真的希望"正常工作"而不是给我错误"无法找到所请求的.Net Framework数据提供程序".

有没有办法在运行时"注册"Npgsql作为数据提供程序,所以这不会发生?

我应该澄清Npgsql在大多数情况下没有在我的machine.config中工作正常,但是有一些它不能很好地工作(比如NLog--我沮丧的当前来源).

c# asp.net postgresql npgsql dataprovider

0
推荐指数
1
解决办法
1502
查看次数

连接池已耗尽

使用并行将数据插入数据库时foreach,出现以下错误:

连接池已耗尽'

将一些数据插入数据库后

try
{
    var connection = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;

    Parallel.ForEach(Enumerable.Range(0, 1000), (_) =>
    {
        using (var connectio = new NpgsqlConnection(connection))
        {
            connectio.Open();
            using (var command = new NpgsqlCommand("fn_tetsdata", connectio) { CommandType = CommandType.StoredProcedure })
            {
                command.Parameters.AddWithValue("firstname", "test");
                command.Parameters.AddWithValue("lastname", "test");
                command.Parameters.AddWithValue("id", 10);
                command.Parameters.AddWithValue("designation", "test");

                command.ExecuteNonQuery();
            }
            connectio.Close();
        }
    });
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
Run Code Online (Sandbox Code Playgroud)

c# npgsql

0
推荐指数
1
解决办法
3180
查看次数

与 Npgsql 和 async 的并发连接数

我正在尝试最大限度地提高 PostgreSQL 数据库的吞吐量,并且可以通过从线程池转移到异步查询来获得巨大的性能飞跃。这async是将执行所述查询的代码版本:

using (var conn = new NpgsqlConnection(connStr)) {
    await conn.OpenAsync();
    var id = await conn.ExecuteScalarAsync<long>(
        "my_proc",
        param_obj,
        commandType: CommandType.StoredProcedure,
        commandTimeout: 0
    );
    return id;
}
Run Code Online (Sandbox Code Playgroud)

以前,当使用线程池时,我可以通过简单地设置线程数来设置任何给定时间的最大打开连接数(如果我超过了 MaxPoolSize,我会看到池耗尽/超时异常,正如预期的那样)。但是,在这种情况下,连接数会不会很快爆炸?也许我不知道有一种更规范的方法可以做到这一点。

c# asynchronous npgsql

0
推荐指数
1
解决办法
3328
查看次数

EF Core - 在 PostgreSQL 13 中将列类型从 varchar 更改为 uuid:列不能自动转换为类型 uuid

前:

public class MyEntity
{
    public string Id { get; set; }
    //...
}
Run Code Online (Sandbox Code Playgroud)

配置:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    //...

    modelBuilder.Entity<MyEntity>()
        .Property(e => e.Id)
        .ValueGeneratedOnAdd();
}
Run Code Online (Sandbox Code Playgroud)

这是先前开发人员的代码,它导致该列的 GUID 值。但是在 C# 中我不得不处理字符串,所以我决定改变模型。

后:

public class MyEntity
{
    public Guid Id { get; set; }
    //...
}
Run Code Online (Sandbox Code Playgroud)

ValueGeneratedOnAdd()从 Fluent API 配置中删除了代码。

我得到了column "Id" cannot be cast automatically to type uuid错误。

我认为这个信息的关键是这个automatically词。

现在我的问题是,由于该列上的值已经是 GUID/UUID,有没有办法告诉 Postgres 将varchar类型更改为uuid并将当前字符串值转换为 UUID 并将其放入列中?我猜应该有一个 SQL 脚本可以做到这一点而不会丢失任何数据。

postgresql npgsql entity-framework-core entity-framework-migrations

0
推荐指数
2
解决办法
1117
查看次数

如何在 FromSqlRaw 查询中转义 { 字符

在使用 Npsql EF Core 数据提供程序的 EF Core 中,查询如

await ctx.Doc.FromSqlRaw(@"select * from Doc where id=any('{1,2,3}')");
Run Code Online (Sandbox Code Playgroud)

抛出错误

索引(从零开始)必须大于或等于零且小于参数列表的大小。

如何在查询中使用{}字符以使它们不被视为参数?

npgsql entity-framework-core .net-core asp.net-core

0
推荐指数
1
解决办法
37
查看次数

升级 .NET Core 时 Postgres 功能不起作用

当我们将 .net core 和其他软件包升级到最新版本时,Postgres 功能无法正常工作。早些时候,相同的代码运行得很好。

我们使用函数而不是存储过程。

DbTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);

try
{
        using (DbCommand command = connection.CreateCommand())
        {
            command.Transaction = transaction;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = Constants.StoredProcedure.SPNAME;
            command.Parameters.Add(new NpgsqlParameter("Param1", NpgsqlTypes.NpgsqlDbType.Integer)
            { Value = val1 });
            command.Parameters.Add(new NpgsqlParameter("Param2", NpgsqlTypes.NpgsqlDbType.Varchar)
            { Value = val2 });
            command.Parameters.Add(new NpgsqlParameter("Param3", NpgsqlTypes.NpgsqlDbType.Varchar)
            { Value = val3 });
            var res = command.ExecuteScalar();
            transaction.Commit();
}}
Run Code Online (Sandbox Code Playgroud)

错误 - Npgsql.PostgresException:'42809:public.NotableEventUserModeratorJoinOrder(Param1 => 整数,Param2 => 字符变化,Param3 => 字符变化)不是过程

我们注释了行command.CommandType = CommandType.StoredProcedure;,然后收到错误 - PostgreSQL,Npgsql 返回 42601:语法错误

postgresql npgsql

0
推荐指数
1
解决办法
1015
查看次数