{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L81-L117","documentation":"CosmosModelValidator.ValidateSharedContainerCompatibility throws InvalidOperationException(CosmosStrings.OwnedTypeDifferentContainer) when an entity type that is owned by another entity type (entityType.FindOwnership() != null) is mapped to a Cosmos container directly. Cosmos owned types must be embedded in the same document as their owner; they cannot live in their own container. The validator catches this at model finalization.","triggerScenarios":"Calling OwnsOne(o => o.Address).ToContainer(\"Addresses\") or OwnsMany(...).HasContainerName(...); configuring an owned entity type with a container name via attribute or IEntityTypeConfiguration; using OwnsOne combined with HasContainerName on the owned type.","commonSituations":"Porting a relational model where owned types were table-splitting into separate tables and converting them to containers; applying a generic 'configure all entity types set their container' routine that also catches owned types.","solutions":["Remove ToContainer/HasContainerName from the owned entity configuration; let the owned type embed inside the owner's document.","If the entity genuinely needs its own container, change it from owned to a regular entity with a one-to-many or one-to-one relationship.","When iterating entity types to apply container configuration generically, skip types where entityType.FindOwnership() != null.","Audit IEntityTypeConfiguration<T> classes that configure owned types and remove any container-name setters."],"exampleFix":"// before - throws: owned type has its own container\nentity.OwnsOne(e => e.Profile, pb =>\n{\n    pb.ToContainer(\"Profiles\");\n});\n\n// after - let the owned type embed in the owner's document\nentity.OwnsOne(e => e.Profile);\n// or: make Profile a real entity with a relationship, then it can have a container\nentity.HasOne(e => e.Profile)\n    .WithOne()\n    .HasForeignKey<Profile>(p => p.OwnerId);","handlingStrategy":"validation","validationCode":"// Skip owned types when applying container configuration generically.\nforeach (var et in modelBuilder.Model.GetEntityTypes().Where(e => e.FindOwnership() is null && e.BaseType is null))\n{\n    modelBuilder.Entity(et.ClrType).HasContainerName(\"Shared\");\n}","typeGuard":"static bool IsOwnedType(IEntityType et) => et.FindOwnership() is not null;","tryCatchPattern":null,"preventionTips":["Do not call ToContainer on owned entity types.","If the type needs its own container, change ownership to a regular relationship.","Skip owned types in any convention that blanket-applies container names.","Audit IEntityTypeConfiguration<T> for owned types and remove ToContainer calls."],"tags":["ef-core","cosmos","model-validation","owned-types","container"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}