cosmos-entity

Design Cosmos DB entities with hierarchical partition keys and discriminators.

4|1|Updated Mar 15, 2026
One-click install
npx skills add https://github.com/FaysilAlshareef/dotnet-ai-kit --skill cosmos-entity
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cosmos-entity
Source: https://github.com/FaysilAlshareef/dotnet-ai-kit/tree/main/skills/microservice/cosmos/cosmos-entity
Command: npx skills add https://github.com/FaysilAlshareef/dotnet-ai-kit --skill cosmos-entity

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Designing and maintaining Cosmos DB document entities that are consistent, queryable, and safe for concurrent updates is challenging without clear conventions for ids, partitioning, discriminators, and concurrency control. This Skill captures a repeatable pattern to ensure entities across a microservice or domain follow the same contract and partition strategy to avoid hot partitions, ambiguous queries, and update conflicts.

Core Features & Use Cases

  • IContainerDocument contract: Standardizes required properties such as ContainerName, PartitionKeys, Discriminator, and ETag so different services and readers can rely on the same shape.
  • Hierarchical partition keys: Recommends a three-level PartitionKeyBuilder strategy to distribute load and enable efficient scoped queries.
  • Polymorphic containers: Uses a Discriminator string to allow multiple entity types to coexist in a single container while supporting polymorphic queries.
  • Optimistic concurrency: Uses ETag for safe updates and recommends factory methods and Apply patterns for event-sourced creation and updates.
  • Anti-pattern guidance: Calls out common mistakes like missing lowercase id, single partition keys, unbounded nested arrays, and public setters without factories.

Quick Start

Create a Cosmos DB entity for sales invoices using IContainerDocument with a three-level partition key, a Discriminator property, lowercase id, ETag support, and factory methods for event-based construction.

Frequently Asked Questions about cosmos-entity

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I design Cosmos DB document entities to avoid hot partitions and update conflicts?

To design resilient Cosmos DB document entities, implement a three-level hierarchical partition key strategy and use ETag for optimistic concurrency. This standardizes entity shapes to distribute load evenly and prevent update conflicts in .NET microservices.

What is the best way to structure polymorphic containers in Cosmos DB?

The best way to structure polymorphic Cosmos DB containers is by using a Discriminator string property. This allows multiple entity types to coexist in a single container while reliably supporting polymorphic queries across your domain.

How do I implement event sourcing with Cosmos DB entities in .NET?

Implement event sourcing in Cosmos DB by using factory methods for entity creation and Apply patterns for updates. This enforces an IContainerDocument contract with lowercase id and ETag properties to safely manage event-based construction.

Why does my Cosmos DB document model fail queries with unbounded nested arrays?

Cosmos DB document models with unbounded nested arrays often fail due to poor structural design and ambiguous queries. Avoid this anti-pattern by enforcing a standard IContainerDocument contract with hierarchical partition keys and discriminators.

Can I use this Cosmos DB document model pattern for .NET microservices?

Yes, this document model pattern applies directly to .NET microservices and domain projects. It ensures entities follow a consistent IContainerDocument contract with PartitionKeyBuilder strategies for efficient, safe concurrent updates.

What are common anti-patterns when creating Cosmos DB document entities?

Common Cosmos DB document entity anti-patterns include missing lowercase id properties, using single partition keys, allowing unbounded nested arrays, and exposing public setters without factory methods, all of which degrade query performance and data consistency.