How to share Item types in the same container? #188
-
Describe the bug To Reproduce Tag:
Category:
Container: "meta" (not pasted an image from the course slide as unsure about copyright etc). Reproduction code: using Microsoft.Azure.CosmosRepository;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
const string databaseName = "datamodelling";
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddCosmosRepository(options =>
{
// do not do this; demo only to make obvious
options.CosmosConnectionString = "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
options.DatabaseId = databaseName;
options.ContainerPerItemType = false;
options.ContainerBuilder.Configure<Category>(builder =>
{
builder.WithContainer("meta")
.WithPartitionKey("/type");
});
options.ContainerBuilder.Configure<Tag>(builder =>
{
builder.WithContainer("meta")
.WithPartitionKey("/type");
});
});
})
.Build();
var tagsRepository = host.Services.GetService<IRepository<Tag>>();
await tagsRepository.CreateAsync(new Tag() { Name = "production" });
await tagsRepository.CreateAsync(new Tag() { Name = "staging" });
await tagsRepository.CreateAsync(new Tag() { Name = "development" });
var categoryRepository = host.Services.GetService<IRepository<Category>>();
await categoryRepository.CreateAsync(new Category() { Name = "Laptops" });
await categoryRepository.CreateAsync(new Category() { Name = "Desktops" });
await categoryRepository.CreateAsync(new Category() { Name = "Workstations" });
var tags = await tagsRepository.GetAsync(t => true);
//[Container("meta")]
//[PartitionKeyPath("/type")]
public class Tag : Item
{
public string Name { get; set; }
protected override string GetPartitionKeyValue()
{
return base.Type;
}
}
//[Container("meta")]
//[PartitionKeyPath("/type")]
public class Category : Item
{
public string Name { get; set; }
protected override string GetPartitionKeyValue()
{
return base.Type;
}
} As you can see from the above I have tried the attributes to define the container and partition keys. I have also tried the builder options to define container and partition key values. And also various combinations of them both, but no luck. Expected behavior Actual behavior Environment summary |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
Hi, since you are using the container builder, you need to set the container per item type property to true, this gives you full control over where each type is stored. Hope this helps, we need to add this to the docs really. |
Beta Was this translation helpful? Give feedback.
-
Maybe because ContainerPerItemType is false? |
Beta Was this translation helpful? Give feedback.
-
Ah OK, will give it a go. The whole item per container set as true but trying to put more than one item in a single container confused me. Will let you know. Thanks 😊 |
Beta Was this translation helpful? Give feedback.
-
Yes, I can see where you're coming from, with that set to |
Beta Was this translation helpful? Give feedback.
-
I've just tried it. Using exactly the same code above but changing ...
... to ...
Thanks for responding 😄 |
Beta Was this translation helpful? Give feedback.
Hi, since you are using the container builder, you need to set the container per item type property to true, this gives you full control over where each type is stored. Hope this helps, we need to add this to the docs really.