{"record":{"id":"d70dd0e9765cfdcd","repo":"dotnet/efcore","slug":"the-entity-type-entitytype-is-owned-by-the-ent","errorCode":null,"errorMessage":"The entity type '{entityType}' is owned by the entity type '{owner}', but is mapped to the container '{container}'. Owned types mapped to a container directly are not supported, remove this configuration to allow the owned type to be embedded in the same document as the owner.","messagePattern":"The entity type '(.+?)' is owned by the entity type '(.+?)', but is mapped to the container '(.+?)'\\. Owned types mapped to a container directly are not supported, remove this configuration to allow the owned type to be embedded in the same document as the owner\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs","lineNumber":99,"sourceCode":"        foreach (var entityType in model.GetEntityTypes().Where(et => et.FindPrimaryKey() != null))\n        {\n            var container = entityType.GetContainer();\n            if (container == null)\n            {\n                continue;\n            }\n\n            if (entityType.BaseType != null\n                && entityType.FindAnnotation(CosmosAnnotationNames.ContainerName)?.Value != null)\n            {\n                throw new InvalidOperationException(\n                    CosmosStrings.ContainerNotOnRoot(entityType.DisplayName(), entityType.BaseType.DisplayName()));\n            }\n\n            var ownership = entityType.FindOwnership();\n            if (ownership != null)\n            {\n                throw new InvalidOperationException(\n                    CosmosStrings.OwnedTypeDifferentContainer(\n                        entityType.DisplayName(),\n                        ownership.PrincipalEntityType.DisplayName(),\n                        container));\n            }\n\n            if (entityType.GetContainingPropertyName() != null)\n            {\n                throw new InvalidOperationException(\n                    CosmosStrings.ContainerContainingPropertyConflict(\n                        entityType.DisplayName(),\n                        container,\n                        entityType.GetContainingPropertyName()));\n            }\n\n            if (!containers.TryGetValue(container, out var mappedTypes))\n            {\n                mappedTypes = [];","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L81-L117","documentation":"Owned types are embedded inside the owner's JSON document in Cosmos; they cannot be mapped to their own container. CosmosModelValidator throws InvalidOperationException via CosmosStrings.OwnedTypeDifferentContainer when an entity type that has an ownership also has a container name configured.","triggerScenarios":"Calling OwnsOne/OwnsMany on an entity AND calling ToContainer/HasContainerName on that same owned entity type (directly or via a convention).","commonSituations":"A global convention that sets a container on every entity type including owned ones; configuring an owned type as if it were an aggregate root; mixing own-and-container after refactoring.","solutions":["Remove the ToContainer/HasContainerName configuration from the owned type so it embeds in the owner's document.","If the type must live in its own container, remove the ownership (make it a regular relationship) instead."],"exampleFix":"// before (owned type also given its own container)\nmodelBuilder.Entity<Order>(o =>\n{\n    o.OwnsOne(p => p.ShippingAddress, sa => sa.ToContainer(\"addresses\"));\n});\n\n// after (owned type embeds in the owner document)\nmodelBuilder.Entity<Order>(o =>\n{\n    o.OwnsOne(p => p.ShippingAddress);\n});","handlingStrategy":"validation","validationCode":"// Owned types must not carry a container name.\nforeach (var et in modelBuilder.Model.GetEntityTypes())\n{\n    if (et.FindOwnership() != null && et.GetContainer() != null)\n        throw new InvalidOperationException($\"Remove ToContainer from owned type {et.Name}.\");\n}","typeGuard":"static bool IsOwnedType(IEntityType et) => et.FindOwnership() != null;","tryCatchPattern":"try { context.Database.EnsureCreated(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Owned types mapped to a container\"))\n{ throw new InvalidOperationException(\"Remove ToContainer from owned types; they embed in the owner.\", ex); }","preventionTips":["Never call ToContainer on an owned type.","Make global container conventions skip types that have an ownership.","If a type needs its own container, remove the OwnsOne/OwnsMany relationship."],"tags":["cosmos","owned-types","container","model-validation","embedding"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}