-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
Question
I'm migrating an EF6 app to a SQL Server and a net 10 ef core
I got a table (Configuration) that contains only 1 records for all configuration of the application
I got a table (FiscalPeriod) that contains all Fiscal Periods of the company
In the tale FiscalPeriod, I got an Id (int), serveral dates..
In the configuration table, I got an Id (UniqueIdentifier) and a one CurrentFiscalId (FK to table FiscalPeriod)
the CurrentFiscalId is not nullable
In the Configuration class, I got the Navigation CurrentFiscal (FiscalPeriod) and the CurrentFiscalId property (int).
In the FiscalPeriod class, I don't have Navigation Property to Configuration
Everythings work fine, I can Add FiscalPeriod and configurationig data in context and save..
But when i want to "change" the current FiscalId to an existing one (ex: pass 3 to 2)
I got an error
"The association between entities 'FiscalPeriod' and 'Configuration' with the key value '{CurrentPeriodYearId: 3}'
has been severed, but the relationship is either marked as required or is implicitly required because the foreign key is not nullable.
If the dependent/child entity should be deleted when a required relationship is severed, configure the relationship to use cascade deletes."
I don't want to delete the row in "FiscalPeriod" only change the link in the Configuration table
I did try serveral thing the the builder
Any Ideas ????
Your code
modelBuilder.Entity<Configuration>(entity =>
{
entity.ToTable("Configuration"),
tb =>tb.HasTrigger("Trigger_Configuration_ProfitCenterUnitDescription"));
entity.HasKey(e => e.UniqueId);
entity.Property(e => e.CurrentPeriodYearId).IsRequired();
// did try .. WithMany and other ...
entity.HasOne(e => e.CurrentPeriodYear)
.WithOne()
.HasForeignKey<Configuration>(e => e.CurrentPeriodYearId)
.HasPrincipalKey<FiscalPeriod>(e => e.PeriodId)
.IsRequired(false)
.HasConstraintName("FK_Configuration_FiscalPeriod");
...
}
modelBuilder.Entity<FiscalPeriod>(entity =>
{
entity.ToTable("FiscalPeriod);
entity.HasKey(e => e.PeriodId);
...
}Stack traces
fail: 2025-12-10 16:50:58.088 CoreEventId.SaveChangesFailed[10000] (Microsoft.EntityFrameworkCore.Update)
An exception occurred in the database while saving changes for context type 'Maximum.Edm.MaximumEntitiesContext'.
System.InvalidOperationException: The association between entities 'FiscalPeriod' and 'AccountingConfiguration' with the key value '{CurrentPeriodYearId: 3}' has been severed, but the relationship is either marked as required or is implicitly required because the foreign key is not nullable. If the dependent/child entity should be deleted when a required relationship is severed, configure the relationship to use cascade deletes.
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.HandleConceptualNulls(Boolean sensitiveLoggingEnabled, Boolean force, Boolean isCascadeDelete)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.CascadeChanges(Boolean force)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.GetEntriesToSave(Boolean cascadeChanges)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(StateManager stateManager, Boolean acceptAllChangesOnSuccess)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
Verbose output
EF Core version
10.0
Database provider
No response
Target framework
net 10
Operating system
No response
IDE
studio 2026