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
I want to have the field ParentNum nullable because it is not required; at the same time I want to use the Children list field via the foreign key.
The problem is that ef core requires all fields from the foreign key being NOT NULL (or [Required]). When I want to enforce this by using the fluent API with .IsRequired(false) I get an exception (see below).
My expected behavior of the fluent API would be that if I use .isRequired(false) on a relationship, all fields are "optional required" (meaning: they are not required but can be made required by other conditions e.g. being a primary key or being defined with the attribute [Required]).
An alternative solution would be to add support to set a condition to the foreign key (similar to #10665).
Then I could just set a condition that the foreign key only applies when the ParentNum is not NULL.
Exception message: System.InvalidOperationException: The property 'CompanyNum' on entity type 'MyEntry' cannot be marked as nullable/optional because it has been included in a key {'CompanyNum', 'Num'}.
Stack trace:
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalPropertyBuilder.IsRequired(Boolean isRequired, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalRelationshipBuilder.IsRequired(Boolean isRequired, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceCollectionBuilder`2.IsRequired(Boolean required)
at DuckAccounting.DataModel.Entites.MyEntry.<>c.<OnModelCreating>b__0_0(EntityTypeBuilder`1 entry)
at Microsoft.EntityFrameworkCore.ModelBuilder.Entity[TEntity](Action`1 buildAction)
at DuckAccounting.DataModel.Entites.MyEntry.OnModelCreating(ModelBuilder modelBuilder)
at DuckAccounting.DataModel.DuckAccountingContext.OnModelCreating(ModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Further technical details
EF Core version: 2.1.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 8.1
IDE: Visual Studio 2017 15.6.6
The text was updated successfully, but these errors were encountered:
@ice1e0 Currently alternate keys have the same requirements as primary keys in that they cannot be nullable. Issue #4415 is tracking relaxing that requirement, so this is essentially a duplicate of that issue.
I want to set up an N-Hierarchy entity as follows:
I want to have the field
ParentNum
nullable because it is not required; at the same time I want to use theChildren
list field via the foreign key.The problem is that ef core requires all fields from the foreign key being
NOT NULL
(or[Required]
). When I want to enforce this by using the fluent API with.IsRequired(false)
I get an exception (see below).My expected behavior of the fluent API would be that if I use
.isRequired(false)
on a relationship, all fields are "optional required" (meaning: they are not required but can be made required by other conditions e.g. being a primary key or being defined with the attribute[Required]
).An alternative solution would be to add support to set a condition to the foreign key (similar to #10665).
Then I could just set a condition that the foreign key only applies when the
ParentNum
is not NULL.Further technical details
EF Core version: 2.1.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 8.1
IDE: Visual Studio 2017 15.6.6
The text was updated successfully, but these errors were encountered: