Home > Blockchain >  Cosmos dbcontext retrieves empty results
Cosmos dbcontext retrieves empty results

Time:11-12

I have a DbContext set up to use Cosmos DB as follows:

public class AuthenticationCosmosDataContext : DbContext, IUnitOfWork
{
    private readonly CosmosOptions cosmosOptions;

    public DbSet<Settings> Settings { get; set; }

    public AuthenticationCosmosDataContext(DbContextOptions<AuthenticationCosmosDataContext> options, IOptions<CosmosOptions> cosmosOptions) : base(options)
    {
        this.cosmosOptions = cosmosOptions.Value;
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultContainer(cosmosOptions.DefaultContainer);
        modelBuilder.Entity<Settings>().ToContainer(cosmosOptions.DefaultContainer);
        modelBuilder.ApplyConfiguration(new SettingsConfiguration());
    }

Which is set up from Startup.cs like this:

services.AddDbContext<AuthenticationCosmosDataContext>((serviceProvider, options) =>
{
   var cosmosOptions = serviceProvider.GetRequiredService<IOptions<CosmosOptions>>().Value;
   options.UseCosmos(cosmosOptions.AccountEndpoint, cosmosOptions.AccountKey, cosmosOptions.DatabaseName);

Now, the SettingsConfiguration doesn't have much in it:

builder.HasKey(x => x.Id).HasName("id");
builder.HasPartitionKey(x => x.Id); 

And the Settings look like this:

public class Settings
{
    public string Id { get; set; }
    public int SomeId { get; private set; }
    public string Type { get; private set; }
    public int AnotherId { get; private set; }

I've tried querying data from the database, buy everything returns either null or 0 results:

await cosmosContext.Settings.FirstOrDefaultAsync().ConfigureAwait(false); // returns null
await cosmosContext.Settings.CountAsync().ConfigureAwait(false); // returns 0

The connection seems to be fine as there are no exceptions and I did have to make some adjustments in my model because there was some mismatch with the documents.

What could be going wrong?


UPDATE: It appears the HasName() method in the configuration is not working as expected. If I rename the field from "Id" to "id" it works as expected. Looking for a way to properly set the configuration

CodePudding user response:

You should set the key using builder.Property(p => p.Id).ToJsonProperty("id");.

CodePudding user response:

Not sure if it helps, but dont you have to overwrite the OnConfiguring Method?:

  public class OptionsContext : DbContext
    {
    #region Configuration
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseCosmos(
            "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R ob0N8A7Cgv30VRDJIWEHLM 4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
            databaseName: "OptionsDB",
            options =>
            {
                options.ConnectionMode(ConnectionMode.Gateway);
                options.WebProxy(new WebProxy());
                options.LimitToEndpoint();
                options.Region(Regions.AustraliaCentral);
                options.GatewayModeMaxConnectionLimit(32);
                options.MaxRequestsPerTcpConnection(8);
                options.MaxTcpConnectionsPerEndpoint(16);
                options.IdleTcpConnectionTimeout(TimeSpan.FromMinutes(1));
                options.OpenTcpConnectionTimeout(TimeSpan.FromMinutes(1));
                options.RequestTimeout(TimeSpan.FromMinutes(1));
            });
    #endregion
}

Also: Can you create elements in the Db from code?

  • Related