You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi. We are trying to scaffold a postgres DB in our C# Blazor app. The DB is pretty simple, but there is a number of jsonb columns on different tables that we are trying to bind to our POCOs.
E.g. we have a test_result table, with 2 jsonb columns runtime_info and test_output.
publicpartialclassTestResult{publicRuntimeInfo?RuntimeInfoJSON{get;set;}publicTestOutput?TestOutputJSON{get;set;}}//along with the defined JSON structurespublicclassRuntimeInfo{[JsonPropertyName("sw_resource_id")]publicstring?SwResourceId{get;set;}}//etc.
And finally, OnModelCreatingPartial:
partialvoidOnModelCreatingPartial(ModelBuildermodelBuilder){modelBuilder.Entity<TestResult>(entity =>{entity.OwnsOne(x =>x.RuntimeInfoJSON, t =>{t.ToJson("runtime_info");});entity.OwnsOne(x =>x.TestOutputJSON, t =>{t.ToJson("test_output");t.OwnsMany(y =>y.Measurements);});});}
Up until here, everything works as expected, classes are populated with the values from the DB.
Then, following the same procedure, we wanted to add another jsonb field custom_config from another table fit_fixtures. We used exactly the same approach:
//EF auto generated code:[Table("fit_fixtures",Schema="fit3")]publicpartialclassFitFixture{[Column("custom_config",TypeName="jsonb")]publicstring?CustomConfig{get;set;}}//manually created partial classpublicpartialclassFitFixture{publicCustomConfig?CustomConfigJSON{get;set;}}publicclassCustomConfig{[JsonPropertyName("bb_communication")]publicBbCommunication?BbCommunication{get;set;}}//etc.//OnModelCreatingPartialmodelBuilder.Entity<FitFixture>(entity =>{entity.OwnsOne(x =>x.CustomConfigJSON, cc =>{cc.ToJson("custom_config");cc.OwnsOne(t =>t.BbCommunication);});});
Bug?
On the first DB query, in the EF initialization stage, we get an exception:
System.ArgumentException: 'An item with the same key has already been added. Key: [custom_config, Column: fit_fixtures.custom_config (jsonb) NonNullable)]'
at System.Collections.Generic.TreeSet`1.AddIfNotPresent(T item)
at System.Collections.Generic.SortedSet`1.Add(T item)
at System.Collections.Generic.SortedDictionary`2.Add(TKey key, TValue value)
at Microsoft.EntityFrameworkCore.Metadata.Internal.RelationalModel.CreateContainerColumn[TColumnMappingBase](TableBase tableBase, String containerColumnName, IEntityType mappedType, IRelationalTypeMappingSource relationalTypeMappingSource, Func`4 createColumn)
at Microsoft.EntityFrameworkCore.Metadata.Internal.RelationalModel.CreateTableMapping(IRelationalTypeMappingSource relationalTypeMappingSource, ITypeBase typeBase, ITypeBase mappedType, StoreObjectIdentifier mappedTable, RelationalModel databaseModel, List`1 tableMappings, Boolean includesDerivedTypes, Nullable`1 isSplitEntityTypePrincipal)
at Microsoft.EntityFrameworkCore.Metadata.Internal.RelationalModel.AddTables(RelationalModel databaseModel, IEntityType entityType, IRelationalTypeMappingSource relationalTypeMappingSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.RelationalModel.Create(IModel model, IRelationalAnnotationProvider relationalAnnotationProvider, IRelationalTypeMappingSource relationalTypeMappingSource, Boolean designTime)
at Microsoft.EntityFrameworkCore.Metadata.Internal.RelationalModel.Add(IModel model, IRelationalAnnotationProvider relationalAnnotationProvider, IRelationalTypeMappingSource relationalTypeMappingSource, Boolean designTime)
at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelRuntimeInitializer.InitializeModel(IModel model, Boolean designTime, Boolean prevalidation)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.Initialize(IModel model, Boolean designTime, IDiagnosticsLogger`1 validationLogger)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, ModelCreationDependencies modelCreationDependencies, Boolean designTime)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(Boolean designTime)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__8_4(IServiceProvider p)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
etc...
Seems like it's a problem with the "two" columns custom_config being defined. If I remove the auto-generated scaffolded column, then it works, there is no exception, the values from the DB are correctly populated. But surely I don't want to fiddle with the auto-generated code...
So, the question is why doesn't the same thing happen with the other columns runtime_info and test_output?
I went looking down the stack and found this piece of code:
When I checked model.GetEntityTypes(), I saw that most of the Owned JSON types stuck together, TestResult entity type (that works fine) came after them, but the problematic FitFixture entity type came before them.
Just for fun, I tried renaming FitFixture -> ZFitFixture, so it came after the owned JSON types and to my surprise it worked.
Please, shed some light onto this conundrum. Is our setup OK? Or is there actually a bug in EF?
Provider and version information
EF Core version: 8.0.10
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL 8.0.10
Database: PostgreSQL 10.23
Target framework: .NET 8
Operating system: Windows 11
IDE: Visual Studio 2022
The text was updated successfully, but these errors were encountered:
Description
Hi. We are trying to scaffold a postgres DB in our C# Blazor app. The DB is pretty simple, but there is a number of jsonb columns on different tables that we are trying to bind to our POCOs.
E.g. we have a
test_result
table, with 2 jsonb columnsruntime_info
andtest_output
.EF successfully scaffolded them into:
Then, we created another partial class, like so:
And finally,
OnModelCreatingPartial
:Up until here, everything works as expected, classes are populated with the values from the DB.
Then, following the same procedure, we wanted to add another jsonb field
custom_config
from another tablefit_fixtures
. We used exactly the same approach:Bug?
On the first DB query, in the EF initialization stage, we get an exception:
Seems like it's a problem with the "two" columns
custom_config
being defined. If I remove the auto-generated scaffolded column, then it works, there is no exception, the values from the DB are correctly populated. But surely I don't want to fiddle with the auto-generated code...So, the question is why doesn't the same thing happen with the other columns
runtime_info
andtest_output
?I went looking down the stack and found this piece of code:
When I checked
model.GetEntityTypes()
, I saw that most of the Owned JSON types stuck together,TestResult
entity type (that works fine) came after them, but the problematicFitFixture
entity type came before them.Just for fun, I tried renaming
FitFixture
->ZFitFixture
, so it came after the owned JSON types and to my surprise it worked.Please, shed some light onto this conundrum. Is our setup OK? Or is there actually a bug in EF?
Provider and version information
EF Core version: 8.0.10
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL 8.0.10
Database: PostgreSQL 10.23
Target framework: .NET 8
Operating system: Windows 11
IDE: Visual Studio 2022
The text was updated successfully, but these errors were encountered: