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.
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)
timestamp with time zone
should be the default for DateTime properties in EF Core, so you should get timestamp without time zone
only if you're explicitly specifying it
timestamp with time zone
should always have Kind=UTC