Npgsql is the .NET data provider for Postgresql. It allows any program developed for .NET framework to access a Postgresql database server. It is implemented in 100% C# code. Postgresql versions since 9.0 are officially supported, others may work.
timestamp without time zone
but when i try to store DateTime.Now in it, I get Cannot write DateTime with Kind=Local to PostgreSQL type 'timestamp with time zone'
why does it give me a error for timestamp with time zone
when I'm using without?
timestamp without time zone
(remember that timestamp with time zone
is the default for .NET DateTime!). See https://github.com/npgsql/efcore.pg/issues/2128#issuecomment-983404118 for more info, if that doesn't help please open an issue with a repro and I'll try to help.
NpgsqlException (0x80004005): Exception while connecting
). I updated to Npgsql 6.0 and the errors changed to Npgsql.NpgsqlException (0x80004005): Exception while reading from stream
. This starts happening at around 400 queries per second. how can I fix this?
timestamp without time zone
column in fact already contains UTC timestamps, set TimeZone to UTC as explained in the link above.
Dispose()
on the object when it goes out of scope.await using
is the async counterpart of the using declaration that tells the compiler to call DisposeAsync()
instead of Dispose()
. See https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-disposeasync#using-async-disposable for more background.
I am feeling really stupid right now, but I don't understand why this doesn't work.
I have a POCO that looks like:
public class DbEquipmentFeature : EntityBase
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DbId { get; set; }
public Guid Id { get; set; } = Guid.NewGuid();
public DbEquipment Equipment { get; set; }
public Guid EquipmentId { get; set; }
[Column(TypeName = "jsonb")]
public SerializableMultiLangString Value { get; set; }
}
and SerializableMultiLangString is
public class SerializableMultiLangString
{
public Translation[] Translations { get; set; }
}
and Translation is
public class Translation
{
public Translation() { }
public LanguageCode LanguageId { get; set; } = 0;
public string LanguageShortCode => LanguageId.ToString();
public string Text { get; set; }
public string Description { get; set; }
}
But I keep getting System.InvalidOperationException: The entity type 'SerializableMultiLangString' requires a primary key to be defined. If you intended to use a keyless entity type call 'HasNoKey()'.
Which doesn't make sense because its not its own entity?
Hey All,
We have a customer and county table. We added below config and Customer.CountryId is optional. we are able to populate CountryDetail if CountryId is not null and get below error if Customer.CountryId is null. How to fix this issue? . TIA.
builder.HasOne(x => x. CountryDetail).WithOne().HasForeignKey< Country >(x => x. Id).HasPrincipalKey<Customer>(z => z. CountryId);
public class Customer {
public int Id {get;set;}
public int? CountryId {get;set;}
public Country? CountryDetail {get;set;}
}
public class Country
{
public int CountryId {get;set;}
public string name {get;set;}
}
Error:
System.InvalidCastException: Column 'CountryId is null.
at Npgsql.ThrowHelper.ThrowInvalidCastException_NoValue(FieldDescription field)
at Npgsql.NpgsqlDataReader.GetFieldValue[T](Int32 ordinal)
at Npgsql.NpgsqlDataReader.GetInt32(Int32 ordinal)
at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.BufferedDataRecord.ReadInt(DbDataReader reader, Int32 ordinal, ReaderColumn column)
at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.BufferedDataRecord.ReadRow()
at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.BufferedDataRecord.InitializeAsync(DbDataReader reader, IReadOnlyList`1 columns, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.InitializeAsync(IReadOnlyList`1 columns, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.InitializeAsync(IReadOnlyList`1 columns, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)