{"record":{"id":"c17cd3e9c4091f17","repo":"dotnet/efcore","slug":"the-default-time-to-live-was-configured-to-ttl1","errorCode":null,"errorMessage":"The default time to live was configured to '{ttl1}' on '{entityType1}', but on '{entityType2}' it was configured to '{ttl2}'. All entity types mapped to the same container '{container}' must be configured with the same default time to live.","messagePattern":"The default time to live was configured to '(.+?)' on '(.+?)', but on '(.+?)' it was configured to '(.+?)'\\. All entity types mapped to the same container '(.+?)' must be configured with the same default time to live\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs","lineNumber":266,"sourceCode":"                            analyticalTtl,\n                            conflictingEntityType.DisplayName(),\n                            entityType.DisplayName(),\n                            currentAnalyticalTtl,\n                            container));\n                }\n            }\n\n            var currentDefaultTtl = entityType.GetDefaultTimeToLive();\n            if (currentDefaultTtl != null)\n            {\n                if (defaultTtl == null)\n                {\n                    defaultTtl = currentDefaultTtl;\n                }\n                else if (defaultTtl != currentDefaultTtl)\n                {\n                    var conflictingEntityType = mappedTypes.First(et => et.GetDefaultTimeToLive() != null);\n                    throw new InvalidOperationException(\n                        CosmosStrings.DefaultTTLMismatch(\n                            defaultTtl,\n                            conflictingEntityType.DisplayName(),\n                            entityType.DisplayName(),\n                            currentDefaultTtl,\n                            container));\n                }\n            }\n\n            var currentThroughput = entityType.GetThroughput();\n            if (currentThroughput != null)\n            {\n                if (throughput == null)\n                {\n                    throughput = currentThroughput;\n                }\n                else if ((throughput.AutoscaleMaxThroughput ?? throughput.Throughput)\n                         != (currentThroughput.AutoscaleMaxThroughput ?? currentThroughput.Throughput))","sourceCodeStart":248,"sourceCodeEnd":284,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L248-L284","documentation":"Thrown when the default time-to-live (DefaultTimeToLive) differs across entity types mapped to the same Cosmos container. Default TTL is a container-level Cosmos setting that auto-expires documents, so every type sharing the container must agree. The validator keeps the first non-null value and throws on the next mismatch.","triggerScenarios":"One entity type calls HasDefaultTimeToLive(TimeSpan) with one value and another type in the same container calls it with a different value (or with null while the first set a non-null value). Fires during model finalization/validation.","commonSituations":"Setting per-type TTLs before realizing the types share a container; migrating from per-entity TTL design to shared-container (TPT-like) design without scrubbing old TTL calls; mixing TimeSpan.Zero (no TTL) with non-zero values across the hierarchy.","solutions":["Choose the single TTL the container must use and apply it once on the document-root entity.","Search for every HasDefaultTimeToLive call against entities mapped to the named container and delete the conflicting ones.","Verify the units: HasDefaultTimeToLive expects a TimeSpan; double-check you did not pass seconds where hours were intended."],"exampleFix":"// before\nmodelBuilder.Entity<User>().ToContainer(\"Users\").HasDefaultTimeToLive(TimeSpan.FromDays(30));\nmodelBuilder.Entity<Guest>().ToContainer(\"Users\").HasDefaultTimeToLive(TimeSpan.FromDays(7));\n\n// after\nmodelBuilder.Entity<User>().ToContainer(\"Users\").HasDefaultTimeToLive(TimeSpan.FromDays(30));\nmodelBuilder.Entity<Guest>().ToContainer(\"Users\");","handlingStrategy":"validation","validationCode":"foreach (var g in dbContext.Model.GetEntityTypes().GroupBy(e => e.GetContainer()))\n{\n    var ttls = g.Select(e => e.GetDefaultTimeToLive()).Where(t => t != null).Distinct().ToList();\n    if (ttls.Count > 1) throw new InvalidOperationException($\"Container {g.Key} has conflicting default TTLs\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Configure HasDefaultTimeToLive once per container on the document root.","Centralize TTL constants in one static class.","Validate the built model in a unit test."],"tags":["cosmos","model-validation","ttl","configuration"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}