标签: npgsql

无法确定类型为“Npgsql.NpgsqlFactory”的提供者工厂的提供者名称

我尝试运行一个测试项目,但我没有成功,因为 npgsql 连接失败。我重新安装了 Npgsql、Npgsql.EntityFramework、EntityFramwork ......但问题仍然存在。结果信息:

测试方法 TestUserControl.ControleBaseTest.TesteGetPermissoesUsuarioFixo gerou exceção:

System.NotSupportedException:无法确定类型为“Npgsql.NpgsqlFactory”的提供程序工厂的提供程序名称。确保在应用程序配置中安装或注册了 ADO.NET 提供程序。结果堆栈跟踪:

我有这个 app.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SqlClient"     type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework">    </provider>
    </providers>
    <!--<defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql" />-->
    <defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, Npgsql" />
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="Npgsql" /> …
Run Code Online (Sandbox Code Playgroud)

c# postgresql asp.net-mvc unit-testing npgsql

5
推荐指数
1
解决办法
3342
查看次数

为什么我的 Web 应用程序使用 ipv6 连接到本地 postgres

所以我一直在开发一个连接到另一台机器上的 postgresql 服务器的 Web 应用程序。一切正常。

然后我将 Web 应用程序部署到运行 postgresql 服务器的同一台计算机上,并收到一条错误消息:

FATAL: 28000: no pg_hba.conf entry for host "fe80::ccee:154f:18f5:418f%11", user "myuser", database "mydb", SSL off 
Run Code Online (Sandbox Code Playgroud)

我的 pg_hba-conf 已经有这一行:

# IPv6 local connections:
host    all             all             ::1/128                 md5  
Run Code Online (Sandbox Code Playgroud)

那应该是 ipv6 的环回地址,对吗?

我可以通过添加这一行来修复它(纯粹猜测):

#host   all             all             fe80::/16               md5
Run Code Online (Sandbox Code Playgroud)

我怀疑这可以让每个人都连接起来吗?

那么问题是为什么要使用 ipv6 而不是 ipv4?

为什么环回不起作用?

postgresql ipv6 npgsql

5
推荐指数
1
解决办法
2994
查看次数

Dapper 参数无法通过 npgsql 连接与 PostgreSQL 一起使用,postgres 匿名函数参数化是否支持?

我正在尝试使用 dapper 来参数化我编写的用于更新插入的 postgres 匿名函数。这是代码:

private static int UpsertProductPrice(
        IDbConnection connection,
        Data.ProductPrice price,
        List<Data.ProductPriceTier> priceTiers)
    {
        string postgres = @"DO $$
        BEGIN 
        UPDATE product_price 
            SET start_date = @StartDate, end_date = @EndDate, price_tier_type = @PriceTierType
            WHERE product_price_external_id = @Id;
        IF found THEN
            RETURN;
        END IF;
        BEGIN
            INSERT INTO product_price(product_price_external_id, start_date, end_date, price_tier_type) 
            VALUES (@Id, @StartDate, @EndDate, @PriceTierType);
            RETURN;
        EXCEPTION WHEN unique_violation THEN END; END$$;";

        int productPriceId = connection.Execute(
            postgres,
            new { Id = price.product_price_external_id, StartDate = price.start_date, EndDate = price.end_date, PriceTierType …
Run Code Online (Sandbox Code Playgroud)

c# postgresql npgsql dapper

5
推荐指数
1
解决办法
3676
查看次数

从 F# 访问显式转换运算符

使用 npgsql 从 postgresql 读取日期值时出现以下错误:

This expression was expected to have type
    DateTime    
but here has type
    NpgsqlTypes.NpgsqlDate  
Run Code Online (Sandbox Code Playgroud)

现在 npgsql 文档引用了正在定义的显式运算符:

[C#]
public static explicit operator DateTime(
    NpgsqlDate date
);
Run Code Online (Sandbox Code Playgroud)

但我不知道如何从 F# 访问它。

有几种笨拙的、简单的方法可以实现我所需要的,但我感到失望和沮丧,因为我无法找到访问内置演员的方法。

我尝试了旧的 Convert.ToDateTime(...),但即使这样也不起作用。

有人有线索吗?谢谢。

f# npgsql

5
推荐指数
1
解决办法
498
查看次数

“错误:57014:由于用户请求而取消语句”Npgsql

我在我的应用程序中遇到了这个幻象问题,其中特定页面(在 ASP.NET MVC 应用程序上)上的每 5 个请求中就有一个抛出此错误:

Npgsql.NpgsqlException: ERROR: 57014: canceling statement due to user request
   at Npgsql.NpgsqlState.<ProcessBackendResponses>d__0.MoveNext()
   at Npgsql.ForwardsOnlyDataReader.GetNextResponseObject(Boolean cleanup)
   at Npgsql.ForwardsOnlyDataReader.GetNextRow(Boolean clearPending)
   at Npgsql.ForwardsOnlyDataReader.Read()
   at Npgsql.NpgsqlCommand.GetReader(CommandBehavior cb)
   ...
Run Code Online (Sandbox Code Playgroud)

在 npgsql github 页面上,我发现了以下错误报告:615

它在那里说:

不管 Dapper 到底发生了什么,在取消命令时肯定存在竞争条件。部分原因是设计使然,因为 PostgreSQL:取消请求是完全“异步的”(它们通过不相关的套接字传递,而不是作为要取消的连接的一部分),并且您不能限制取消生效仅在特定命令上。换句话说,如果您想取消命令 A,则在您发送取消时,命令 B 可能已经在进行中,它将被取消。

尽管他们在 Npgsql 3.0.2 中进行“希望使取消更安全的更改”,但我当前的代码与此版本不兼容,因为这里描述了迁移的需要。

我目前的解决方法(愚蠢):我已经评论了 Dapper 中的代码,command.Cancel();问题似乎消失了。

if (reader != null)
                {
                    if (!reader.IsClosed && command != null)
                    {
                        //command.Cancel();
                    }
                    reader.Dispose();
                    reader = null;
                }
Run Code Online (Sandbox Code Playgroud)

有没有更好的解决问题的方法?其次,我在当前修复中丢失了什么(除了每次更新 …

postgresql command npgsql dapper

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

PostgreSQL,Npgsql 返回 42601:“$1”处或附近的语法错误

我正在尝试使用 Npgsql 和/或 Dapper 来查询表,但我一直遇到 Npgsql.PostgresException 42601: syntax error at or near "$1".

这是我使用 NpgsqlCommand 进行的尝试:

  using (var conn = new NpgsqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["postgres"].ConnectionString))
  {
    conn.Open();
    using (NpgsqlCommand command = new NpgsqlCommand("select * from Logs.Logs where Log_Date > current_date - interval @days day;", conn))
    {
      command.Parameters.AddWithValue("@days", days);
      var reader = command.ExecuteReader();
Run Code Online (Sandbox Code Playgroud)

我也用 Dapper(我的首选方法)尝试过:

  using (var conn = new NpgsqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["postgres"].ConnectionString))
  {
    conn.Open();
    var logs = conn.Query<Log>("select * from Logs.Logs where Log_Date > current_date - interval @days day;", new {days = days});
Run Code Online (Sandbox Code Playgroud)

无论哪种方式,我都会得到相同Npgsql.PostgresException …

postgresql npgsql dapper

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

Npgsql / EF 6 - json 列

我正在尝试使用 JSON 列创建迁移。这是我尝试过的:

    [Column(TypeName = "Jsonb")]
    public string Data { get; set; }

    [Column(TypeName = "Json")]
    public string Data { get; set; }

    modelBuilder.Entity<Member>().Property(p => p.Data).HasColumnType("Json");

    modelBuilder.Entity<Member>().Property(p => p.Data).HasColumnType("Jsonb");
Run Code Online (Sandbox Code Playgroud)

什么都不起作用,这是例外:

System.InvalidOperationException: Sequence 在 System.Linq.Enumerable.Single[TSource](IEnumerable1 source, Func2 谓词) at System.Data.Entity.Utilities.DbProviderManifestExtensions.GetStoreTypeFromName(DbProviderManifest providerManifest, String name) at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.ConfigureColumn(EdmProperty column, EntityType table, DbestProviderManifest providerManifest在 System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(EdmProperty 列,EntityType 表,DbProviderManifest providerManifest,Boolean allowOverride,Boolean fillFromExistingConfiguration)在 System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration .<>c__DisplayClass4.b__3(Tuple 2 pm) at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable1 ts, Action1 action) at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(IEnumerable1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConfiguration) at System.Data.Entity.ModelConfiguration.Configuration.Types.StructuralTypeConfiguration.ConfigurePropertyMappings(IList …

c# asp.net-mvc entity-framework npgsql entity-framework-migrations

5
推荐指数
1
解决办法
1423
查看次数

如何从 Npgsql 异常中判断该调用是否值得重试(瞬态故障策略)

我正在编写一个将连接到远程 postgres 服务器的服务。我正在寻找一种好方法来确定应将哪些异常视为瞬态(值得重试),以及如何定义适当的策略以连接到远程数据库。

该服务使用 Npgsql 进行数据访问。文档说 Npgsql 将抛出一个 PostgresException 的 sql 错误和一个 NpgsqlException 的“服务器相关问题”。

到目前为止,我能想到的最好的方法是假设所有不是 PostgresExceptions 的异常都应该被视为可能是暂时的,值得重试,但是 PostgresException 意味着查询有问题,重试无济于事. 我在这个假设中正确吗?

我正在使用 Polly 创建重试和断路器策略。因此,我的政策是这样的:

Policy.Handle<Exception>( AllButPotgresExceptions()) // if its a postgres exception we know its not going to work even with a retry, so don't
                       .WaitAndRetryAsync(new[]
                       {
                           TimeSpan.FromSeconds(1),
                           TimeSpan.FromSeconds(2),
                           TimeSpan.FromSeconds(4)
                       }, onRetry: (exception, span) => Log.Warning(exception, "Postgres Retry Failure: "))
                    .WrapAsync(
                           Policy.Handle<Exception>( AllButPotgresExceptions())
                               .AdvancedCircuitBreakerAsync(
                                   failureThreshold:.7, 
                                   samplingDuration: TimeSpan.FromSeconds(30), 
                                   minimumThroughput: 20, 
                                   durationOfBreak: TimeSpan.FromSeconds(30), 
                                   onBreak: (ex, timeSpan, context) => Log.Warning(ex, "Postres Circuit Breaker Broken: …
Run Code Online (Sandbox Code Playgroud)

c# postgresql npgsql microservices polly

5
推荐指数
1
解决办法
2315
查看次数

EF6 + npgsql + CodeFirst 不适用于几何

我们正在尝试将 EF6 + CodeFirst 与 PostGIS 数据库一起使用。

PostgreSQL v9.3.10
PostGIS v2.1.3
npgsql v3.2.2.0
EntityFramework6.Npgsql v3.1.1.0
Run Code Online (Sandbox Code Playgroud)

我没有几何图形的简单模型可以正常工作,Add-Migrations但是当我添加DbGeometry属性时Add-Migrations失败

Not supported edm type: Edm.Geometry
at Npgsql.NpgsqlProviderManifest.GetStoreType(TypeUsage edmType)
at System.Data.Entity.ModelConfiguration.Edm.Services.StructuralTypeMappingGenerator.MapTableColumn(EdmProperty property, String columnName, Boolean isInstancePropertyOnDerivedType)
Run Code Online (Sandbox Code Playgroud)

谷歌搜索我在 npgsql 的 GitHub 上找到了这些帖子:https : //github.com/npgsql/npgsql/issues/623https://github.com/npgsql/EntityFramework6.Npgsql/issues/18

这些帖子是 2015 年 5 月的。我无法想象还没有人找到解决这个问题的方法。我们不能成为多年来第一个EF6+CodeFirstPostGIS.

因此,请建议如何继续或建议其他正在运行的提供商。

postgis npgsql entity-framework-6 entity-framework-migrations

5
推荐指数
0
解决办法
563
查看次数

如何在 ASP.NET Boilerplate Core 2.0 模板中使用 Npgsql?

我花了很多时间尝试将 PostgreSQL 与 ASP.NET Boilerplate Core 2.x + Module Zero(看起来很棒!)一起使用。

  1. 我将Npgsql.EntityFrameworkCore.PostgreSQL&Npgsql.EntityFrameworkCore.PostgreSQL.Design依赖添加到myApp.EntityFrameworkCore.

  2. 我还更改了连接字符串,我使用builder.UseNpgsql而不是builder.UseSqlServerin myappDbContextConfigurer.cs

作为一个新项目,我从Npgsql v2.0.0. 但是经过几个问题,我发现这个版本有一些问题。所以我回到了1.1.1发布。

我很确定这部分工作正常。下一步是删除所有现有迁移并重新加载它们:

  • Add-Migration "Initial_Migrations"
  • Add-Migration "AbpZero_Initial"

当我做第一个时,我收到一个错误:

来自程序集“Npgsql.EntityFrameworkCore.PostgreSQL, Version=1.1.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7”的“Microsoft.EntityFrameworkCore.Infrastructure.Internal.NpgsqlOptionsExtension”类型中的方法“Clone”没有实现。

我在这里找到了解决方案的开始(第 6 篇文章):https : //forum.aspnetboilerplate.com/viewtopic.php?t=5304&p=13013

帖子似乎有点过时,我不知道在哪里实施NpgsqlMigrationSqlGenerator(我的意思是,在哪个项目中)。

感谢您阅读这篇文章,任何帮助将不胜感激。

migration postgresql npgsql aspnetboilerplate asp.net-core-2.0

5
推荐指数
1
解决办法
2111
查看次数