Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve exception for incorrect FK value #9696

Closed
AndriySvyryd opened this issue Sep 5, 2017 · 0 comments
Closed

Improve exception for incorrect FK value #9696

AndriySvyryd opened this issue Sep 5, 2017 · 0 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@AndriySvyryd
Copy link
Member

When the FK is set to a value that corresponds to an entity of an incompatible type the exception is not helpful:

Add eagle.EagleId = kiwi.Species; to InheritanceContext.SeedData

System.InvalidCastException : Unable to cast object of type 'Microsoft.EntityFrameworkCore.TestModels.Inheritance.Kiwi' to type 'Microsoft.EntityFrameworkCore.TestModels.Inheritance.Eagle'.
C:\enlistments\Universe\EntityFramework\src\EFCore\Metadata\Internal\ClrICollectionAccessor.cs(146,0): at Microsoft.EntityFrameworkCore.Metadata.Internal.ClrICollectionAccessor3.GetCollection(Object instance) C:\enlistments\Universe\EntityFramework\src\EFCore\Metadata\Internal\ClrICollectionAccessor.cs(122,0): at Microsoft.EntityFrameworkCore.Metadata.Internal.ClrICollectionAccessor3.GetOrCreateCollection(Object instance)
C:\enlistments\Universe\EntityFramework\src\EFCore\Metadata\Internal\ClrICollectionAccessor.cs(55,0): at Microsoft.EntityFrameworkCore.Metadata.Internal.ClrICollectionAccessor3.Add(Object instance, Object value) C:\enlistments\Universe\EntityFramework\src\EFCore\ChangeTracking\Internal\NavigationFixer.cs(887,0): at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.AddToCollection(InternalEntityEntry entry, INavigation navigation, IClrCollectionAccessor collectionAccessor, Object value) C:\enlistments\Universe\EntityFramework\src\EFCore\ChangeTracking\Internal\NavigationFixer.cs(925,0): at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.SetReferenceOrAddToCollection(InternalEntityEntry entry, INavigation navigation, IClrCollectionAccessor collectionAccessor, Object value) C:\enlistments\Universe\EntityFramework\src\EFCore\ChangeTracking\Internal\NavigationFixer.cs(768,0): at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.ToDependentFixup(InternalEntityEntry dependentEntry, InternalEntityEntry principalEntry, IForeignKey foreignKey) C:\enlistments\Universe\EntityFramework\src\EFCore\ChangeTracking\Internal\NavigationFixer.cs(554,0): at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.InitialFixup(InternalEntityEntry entry, ISet1 handledForeignKeys, Boolean fromQuery)
C:\enlistments\Universe\EntityFramework\src\EFCore\ChangeTracking\Internal\NavigationFixer.cs(474,0): at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)
C:\enlistments\Universe\EntityFramework\src\EFCore\ChangeTracking\Internal\InternalEntityEntryNotifier.cs(95,0): at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)
C:\enlistments\Universe\EntityFramework\src\EFCore\ChangeTracking\Internal\InternalEntityEntry.cs(258,0): at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges)
C:\enlistments\Universe\EntityFramework\src\EFCore\ChangeTracking\Internal\InternalEntityEntry.cs(79,0): at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean forceStateWhenUnknownKey)
C:\enlistments\Universe\EntityFramework\src\EFCore\ChangeTracking\Internal\EntityGraphAttacher.cs(68,0): at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode node)
C:\enlistments\Universe\EntityFramework\src\EFCore\ChangeTracking\Internal\EntityEntryGraphIterator.cs(25,0): at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph(EntityEntryGraphNode node, Func2 handleNode) C:\enlistments\Universe\EntityFramework\src\EFCore\ChangeTracking\Internal\EntityGraphAttacher.cs(34,0): at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState entityState, Boolean forceStateWhenUnknownKey) C:\enlistments\Universe\EntityFramework\src\EFCore\DbContext.cs(601,0): at Microsoft.EntityFrameworkCore.DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState) C:\enlistments\Universe\EntityFramework\src\EFCore\DbContext.cs(805,0): at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState) C:\enlistments\Universe\EntityFramework\src\EFCore\DbContext.cs(651,0): at Microsoft.EntityFrameworkCore.DbContext.Add[TEntity](TEntity entity) C:\enlistments\Universe\EntityFramework\src\EFCore\Internal\InternalDbSet.cs(129,0): at Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.Add(TEntity entity)

For 1-1 a similar exception is thrown:

System.InvalidCastException: Unable to cast object of type 'Microsoft.EntityFrameworkCore.ModelBuilding.ModelBuilderTest.SpecialOrder' to type 'Microsoft.EntityFrameworkCore.ModelBuilding.ModelBuilderTest.BackOrder'.
at Microsoft.EntityFrameworkCore.Metadata.Internal.ClrPropertySetter2.SetClrValue(Object instance, Object value) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalMixedEntityEntry.WritePropertyValue(IPropertyBase propertyBase, Object value) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetProperty(IPropertyBase propertyBase, Object value, Boolean setModified) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.set_Item(IPropertyBase propertyBase, Object value) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.SetNavigation(InternalEntityEntry entry, INavigation navigation, Object value) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.InitialFixup(InternalEntityEntry entry, ISet1 handledForeignKeys, Boolean fromQuery)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode node)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph(EntityEntryGraphNode node, Func2 handleNode) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState entityState, Boolean forceStateWhenUnknownKey) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.NavigationCollectionChanged(InternalEntityEntry entry, INavigation navigation, IEnumerable1 added, IEnumerable1 removed) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.NavigationCollectionChanged(InternalEntityEntry entry, INavigation navigation, IEnumerable1 added, IEnumerable`1 removed)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.DetectNavigationChange(InternalEntityEntry entry, INavigation navigation)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.DetectChanges(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.DetectChanges(IStateManager stateManager)
at Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker.DetectChanges()
at Microsoft.EntityFrameworkCore.DbContext.TryDetectChanges()
at Microsoft.EntityFrameworkCore.DbContext.Entry[TEntity](TEntity entity)

@AndriySvyryd AndriySvyryd changed the title Improve exception forincorrect FK value Improve exception for incorrect FK value Sep 5, 2017
@ajcvickers ajcvickers added this to the 2.1.0 milestone Sep 11, 2017
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview1, 2.1.0 Jan 17, 2018
AndriySvyryd added a commit that referenced this issue Feb 6, 2018
Update transient error message
Add sensitive data to the conceptual null exception
Don't validate mapping for shadow properties created by convention
Improve incompatible principal entity during fixup exception
Correct property is already a navigation exception

Fixes #8363
Fixes #8365
Fixes #9696
Fixes #9817
Fixes #10135
Fixes #10856
AndriySvyryd added a commit that referenced this issue Feb 6, 2018
Update transient error message
Add sensitive data to the conceptual null exception
Don't validate mapping for shadow properties created by convention
Improve incompatible principal entity during fixup exception
Correct property is already a navigation exception

Fixes #8363
Fixes #8365
Fixes #9696
Fixes #9817
Fixes #10135
Fixes #10856
AndriySvyryd added a commit that referenced this issue Feb 6, 2018
Update transient error message
Add sensitive data to the conceptual null exception
Don't validate mapping for shadow properties created by convention
Improve incompatible principal entity during fixup exception
Correct property is already a navigation exception

Fixes #8363
Fixes #8365
Fixes #9696
Fixes #9817
Fixes #10135
Fixes #10856
@AndriySvyryd AndriySvyryd removed their assignment Feb 6, 2018
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Feb 6, 2018
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview2, 2.1.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

2 participants