标签: npgsql

无法首先使用Npgsql和Entity Framework代码在PostreSQL中创建数据库

我正在尝试设置我的应用程序以使用Entity Framework和PostgreSQL,但我遇到了一个问题.我已经Npqsql通过nuget添加了,并将以下提供程序工厂添加到web.config:

<system.data>
    <DbProviderFactories>
        <add name="Npgsql Data Provider" 
           invariant="Npgsql" 
           description=".Net Framework Data Provider for Postgresql Server" 
           type="Npgsql.NpgsqlFactory, Npgsql, 
                 Version=2.0.12.0, Culture=neutral, 
                 PublicKeyToken=5d8b90d52f46fda7" />
    </DbProviderFactories>
  </system.data>
Run Code Online (Sandbox Code Playgroud)

使用连接字符串:

    <add name="MyDb" providerName="Npgsql" 
connectionString="Server=localhost;Port=5432;Database=withoomph;User ID=postgres;Password=******;enlist=true" />
Run Code Online (Sandbox Code Playgroud)

它似乎很好地连接到数据库,但当我尝试在数据库上执行任何类型的操作时,我得到以下异常:

FATAL: 3D000: database "withoomph" does not exist

当db设置如下时,我正在正确设置数据库intitializer:

static MyDB()
{
    Database.SetInitializer<MyDB>(new CreateDatabaseIfNotExists<MyDB>());
}
Run Code Online (Sandbox Code Playgroud)

所以当我尝试用我的DbContext权利做任何事情时,它应该只是简单地创建数据库?我不明白,整个早上都拔我的头发!

c# postgresql entity-framework npgsql

6
推荐指数
1
解决办法
5793
查看次数

PostgreSQL SSPI身份验证 - 致命:2801:用户"xxx"的密码身份验证失败

我对PostgreSQL很陌生,对MS SQL Server有相当多的经验.引起我注意的PostgreSQL的一个特性是SSPI身份验证,我希望这样可以更轻松地从MS SQL过渡.但是,我似乎无法让它发挥作用.

目前,服务器和客户端都在同一台Windows 7上运行,该机器不是域的成员.如果我理解正确,SSPI auth从Kerberos回退到NTLM,所以它应该在没有域的情况下工作 - 我是对的吗?

当我尝试连接(从.NET应用程序通过Npgsql)时,我得到一条NpgsqlException消息:Fatal: 28P01: password authentication failed for user "xxx"消息对我来说有点令人费解,因为我没有使用密码验证.

我的pg_hba.conf:

host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5
host    all             all             127.0.0.1/32            sspi
Run Code Online (Sandbox Code Playgroud)

Npgsql连接字符串:

Server=127.0.0.1;Port=5432;Database=mydb;Integrated Security=true;

我在这做错了什么?或者是没有域名它不起作用?

sql authentication postgresql npgsql

6
推荐指数
1
解决办法
3987
查看次数

在Npgsql中是否有任何SQLBulkCopy的等价物

经过一番搜索后,我找不到在Npgsql中从SQLClient中正确替换SQLBulkCopy.任何人都可以建议我像PostBSQL的SQLBulkCopy吗?我需要插入大约1000万个数据..我知道单个查询插入...

我只是为了比较而做测试.

任何建议表示赞赏.

c# sql-server postgresql npgsql

6
推荐指数
1
解决办法
2101
查看次数

使用ADO.NET进行PostgreSQL参数化插入

我正在使用NpgSQL与PostgreSQL和ADO.NET.原谅这个问题的简单性,因为我本周刚刚开始使用PostgreSQL和NpgSQL.

这样的事情很好:

[Test]
public void InsertNoParameters()
{
    NpgsqlConnection conn = new NpgsqlConnection("Host=localhost; Database=postgres; User ID=postgres; Password=password");
    conn.Open();

    IDbCommand command = conn.CreateCommand();
    string sql = "INSERT INTO Customers (FirstName,LastName) VALUES ('Test','Tube')";
    command.CommandText = sql;
    command.ExecuteNonQuery();
    conn.Close();
}
Run Code Online (Sandbox Code Playgroud)

当我输入参数时,我收到错误消息:Npgsql.NpgsqlException:错误:42703:列"_firstname"不存在

[Test]
public void InsertWithParameters()
{
NpgsqlConnection conn = new NpgsqlConnection("Host=localhost; Database=postgres; User ID=postgres; Password=password");
conn.Open();

IDbCommand command = conn.CreateCommand();
string sql = "INSERT INTO Customers (FirstName,LastName) VALUES (_FirstName,_LastName)";
command.CommandText = sql;

var parameter = command.CreateParameter();
parameter.ParameterName = "_FirstName";
parameter.Value = "Test";
command.Parameters.Add(parameter);

parameter …
Run Code Online (Sandbox Code Playgroud)

postgresql npgsql

6
推荐指数
1
解决办法
7203
查看次数

Npgsql提供程序是否支持TransactionScope?

我正在尝试与Npgsql提供程序一起使用TransactionScope。我在一个古老的问题(.net中的PostgreSQL提供程序,支持TransactionScope)中发现Npgsql还不支持它。现在,大约5年后,Npgsql是否支持TransactionScope?我使用Npgsql 3.0.3并使用以下代码对自己进行了测试:

using (var scope = new TransactionScope())
{
    using(var connection = new Npgsql.NpgsqlConnection("server=localhost;user id=*****;password=*****database=test;CommandTimeout=0"))
    {
        connection.Open();

        var command = new NpgsqlCommand(@"insert into test.table1 values ('{10,20,30}', 2);");
        command.Connection = connection;
        var result = command.ExecuteNonQuery();

        // scope.Complete();  <-- Not committed
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以确认Npgsql不支持TransactionScope吗?

编辑#1 确认Npgsql对TransactionScope的支持后,我发现您需要确保在PostgreSQL配置中启用了分布式事务,请检查postgres.conf文件中的max_prepared_transactions参数(记住要重新启动服务器) 。

编辑#2 我在服务器上启用了分布式事务,但是现在在使用TransactionScope和Npgsql时出现错误。这是我的代码:

using (var sourceDbConnection = new NpgsqlConnection(SourceConnectionString))
using (var destinationDbConnection = new NpgsqlConnection(DestinationConnectionString))
using (var scope = new TransactionScope())
    {
        sourceDbConnection.Open();
        destinationDbConnection.Open();

        Logger.Info("Moving data for the {0} table.", …
Run Code Online (Sandbox Code Playgroud)

npgsql

6
推荐指数
1
解决办法
4031
查看次数

在PowerBI中安装ngpsql以使用PostgreSQL

我正在尝试将数据从PostgreSQL数据库获取到PowerBI Desktop,但它似乎不起作用。我认为问题在于使用npgsql进行安装。当尝试在PowerBI中设置新的数据库连接时,我输入数据库和服务器,这是我得到的错误消息:

“无法连接详细信息:” PostgreSQL:我们找不到具有不变名称'Npgsql'的数据库提供程序。可能是由于需要特定于提供程序的客户端软件而导致的,但此计算机上没有此错误。要下载此提供程序的客户端软件,请访问以下网站,并至少选择64位(x64)版本的“ Npgsql版本2.0.12”:https ://go.microsoft.com/fwlink/?LinkID = 282716 “”

我试过去npgsql网站并安装所需的组件,但似乎无法正常工作。我不是开发人员,所以这对我来说有点复杂。有谁知道如何安装这些组件?非常感谢您的帮助!

如果有帮助,我将在Surface Pro 3上使用Windows 10。而且我也安装了Visual Studio Community。

最好,布鲁诺。

npgsql

6
推荐指数
2
解决办法
6987
查看次数

Asp.Net核心如何更换配置管理器

我是ASP.NET Core RC2的新手,我想知道如何获得一些配置设置并将其应用到我的方法中.对于我的实例appsettings.json我有这个特定的设置

"ConnectionStrings": {
    "DefaultConnection": 
        "Server=localhost;User Id=postgres;port=5432;Password=castro666;Database=dbname;"
  }
Run Code Online (Sandbox Code Playgroud)

在我的Controller中,每次我想查询数据库时,我都必须使用此设置

 using (var conn = 
     new NpgsqlConnection(
         "Server=localhost;User Id=postgres;port=5432;Password=castro666;Database=dbname;"))
 {
     conn.Open();
 }
Run Code Online (Sandbox Code Playgroud)

这里显而易见的是,如果我想在配置中添加更多内容,我必须更改该方法的每个实例.我的问题是如何才能进入DefaultConnection,appsettings.json这样我才能做到这一点

 using (var conn = 
     new NpgsqlConnection(
         ConfigurationManager["DefaultConnection"))
 {
     conn.Open();
 }
Run Code Online (Sandbox Code Playgroud)

c# asp.net npgsql asp.net-core asp.net-core-1.0

6
推荐指数
2
解决办法
9124
查看次数

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
查看次数

如何在 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
查看次数