Background:
I am just starting out with Azure CosmosDB and trying to make use of the Entity Framework CosmosDB provider. I have created a simple API, along with Swagger API documentation, which will operate on a single entity.
So far I have done the following:
- Configured a new Cosmos DB database + container
- Registered CosmosDB into the services container in Startup.cs
- Configured an entity that I want to store in my new Cosmos DB container
- Prepared API endpoints for basic CRUD operations
Problem:
When attempting to call my POST endpoint through Swagger to create a new record in my database, I am getting the following error:
Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: BadRequest (400); Substatus: 1001; ActivityId: fe27e816-173c-433e-8699-e9e49e01b96f; Reason: (Message: {"Errors":["PartitionKey extracted from document doesn't match the one specified in the header"]}
From following tutorials and documentation, it's not obvious why I am getting this error! Any pointers in the right direction would be much appreciated!
Potentially useful snippets that might help someone diagnose where I am going wrong:
Registering Cosmos DB to the service container:
var settings = new CosmosDbOptions();
configuration.GetSection(CosmosDbOptions.SECTION_NAME).Bind(settings);
services.AddDbContext<DatabaseContext>(options =>
{
options.UseCosmos(
accountEndpoint: settings.EndpointUri,
accountKey: settings.PrimaryKey,
databaseName: settings.DatabaseName
);
});
Entity:
public class Client : Entity, IGuidIdentifier, IAggregateRoot
{
public Client() : base()
{
this.Id = Guid.NewGuid();
this.ClientId = this.Id.ToString();
}
public string ClientId { get; private set; }
public IrisCode IrisCode { get; private set; }
public string Name { get; private set; }
public Office Office { get; private set; }
public Logo Logo { get; private set; }
}
Entity framework confgiguration for my entity:
public class ClientConfig : IEntityTypeConfiguration<Client>
{
public void Configure(EntityTypeBuilder<Client> builder)
{
builder.ToContainer("Clients");
builder.HasPartitionKey(x => x.ClientId);
builder.HasKey(x => x.Id);
builder.Property(x => x.Name);
builder.Property(x => x.ClientId);
builder.OwnsOne(x => x.IrisCode);
builder.OwnsOne(x => x.Office);
builder.OwnsOne(x => x.Logo);
builder.Ignore(x => x.DomainEvents);
}
}
Update: It seems that the following line is causing the error, however, this leaves me in a position without having my desired partition key defined..
builder.HasPartitionKey(x => x.ClientId);
ClientId
property is being set in the constructor so it defo has a value) 2.context.Clients.Add(client)
– Tomukebuilder.HasPartitionKey(x => x.ClientId);
was causing the problem. I remove this, and it works! However, I no longer have the desired partition key... any ideas? – Tomuke