{"record":{"id":"8e84b83148a0da57","repo":"dotnet/efcore","slug":"the-object-name-has-been-removed-from-the-mode","errorCode":null,"errorMessage":"The object '{name}' has been removed from the model.","messagePattern":"The object '(.+?)' has been removed from the model\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Metadata/Internal/EntityTypeMappingFragment.cs","lineNumber":51,"sourceCode":"        ConfigurationSource configurationSource)\n    {\n        EntityType = entityType;\n        StoreObject = storeObject;\n        _configurationSource = configurationSource;\n        _builder = new InternalEntityTypeMappingFragmentBuilder(this, ((IConventionModel)entityType.Model).Builder);\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual InternalEntityTypeMappingFragmentBuilder Builder\n    {\n        [DebuggerStepThrough]\n        get => _builder\n            ?? throw new InvalidOperationException(\n                CoreStrings.ObjectRemovedFromModel(\n                    StoreObject.DisplayName()));\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual bool IsInModel\n        => _builder is not null\n            && ((IConventionAnnotatable)EntityType).IsInModel;\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Metadata/Internal/EntityTypeMappingFragment.cs#L33-L69","documentation":"An EntityTypeMappingFragment (the per-store-object mapping slice for an entity) tracks whether it is still attached to the model via its _builder field. Accessing the Builder after the fragment has been removed throws ObjectRemovedFromModel, signaling stale metadata use.","triggerScenarios":"Holding a reference to a fragment returned by EntityType.MappingFragment or IReadOnlyEntityType.FindMappingFragment and then calling modelBuilder to remove it (or removing the entity/store object) before accessing its Builder.","commonSituations":"Conditional model building where a fragment is added then removed in a later pass; caching metadata objects across model rebuilds; using a fragment after EntityType.SetMappingFragment(null, ...).","solutions":["Do not retain fragment references across model mutations; fetch them fresh from the current entity type after any model change.","Before accessing Builder, check fragment.IsInModel (which returns _builder is not null).","Reorder configuration so removals happen last and no later code touches removed fragments."],"exampleFix":"// before\nvar fragment = entityType.FindMappingFragment(table);\nmodelBuilder.Entity<Order>().Ignore(...); // invalidates fragment\nvar b = fragment.Builder; // throws\n\n// after\nmodelBuilder.Entity<Order>().Ignore(...);\nvar freshFragment = modelBuilder.Entity<Order>().Metadata.FindMappingFragment(table);\nvar b = freshFragment?.IsInModel == true ? freshFragment.Builder : null;","handlingStrategy":"type-guard","validationCode":"var fragment = entityType.FindMappingFragment(table);\nif (fragment is { IsInModel: true } attached)\n{\n    var builder = attached.Builder;\n}","typeGuard":"static bool IsFragmentAttached(IReadOnlyEntityTypeMappingFragment f)\n    => f is EntityTypeMappingFragment { IsInModel: true };","tryCatchPattern":null,"preventionTips":["Never cache fragment references across model mutations; fetch them fresh.","Always check IsInModel before accessing Builder.","Reorder configuration to perform removals last."],"tags":["relational","metadata","model-building","lifecycle"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}