RavenDB Asp.Net Core - querying documents throws convert exception

Viewed 143

I am using v5.1 of RavenDB client for C# (.Net 5 - the latest release). My ravendb server (ravendb/ravendb:5.1-ubuntu-latest) + app are running in linux docker containers.

On my app startup, I want to seed my ravendb instance with some data. I initialize the database if it does not exist, try to check existence of any entities, and seed the database if empty.

public async Task SeedIngredientsAsync()
        {
            //ensure database exists first
            await this.EnsureDatabaseExistsAsync();

            IngredientViewDto? existingIngredient = null;
  
            try
            {
                //below query throws exception
                existingIngredient = await _session.Query<IngredientViewDto>()
                                                   .FirstOrDefaultAsync();

                _logger.LogInformation($"Ingredient exist - {existingIngredient}");
            }
            catch (Exception e)
            {
                throw new CreateDatabaseException($"Could not check if ingredient exists -{e.Message}");
            }

            if (existingIngredient == null)
            {
                BulkInsertOperation? bulkInsert = null;

                try
                {
                    _logger.LogError("Inserting ingredients...");

                    var ingredientsToInsert = this.GenerateIngredients();

                    bulkInsert = _session.Advanced.DocumentStore.BulkInsert(_session.Advanced.DocumentStore.Database);

                    for (int i = 0; i < ingredientsToInsert.Count; i++)
                    {
                        var ingredient = ingredientsToInsert[i];
                        await bulkInsert.StoreAsync(ingredient, ingredient.Id.ToString());
                    }
                }
                catch (Exception e)
                {
                    throw new CreateDatabaseException($"Could not seed view database - {e.Message}");
                }
                finally
                {
                    await _session.SaveChangesAsync();

                    if (bulkInsert != null)
                    {
                        await bulkInsert.DisposeAsync().ConfigureAwait(false);
                    }

                    _logger.LogError("Ingredients inserted...");
                }
            }
        }

When I run above, I always get an exception when trying to query my entities within the db

Could not convert document 6d19cb32-8597-4406-9fce-86505da62a6f to entity of type IngredientViewDto

My entity is a POCO:

public class IngredientViewDto
    {
        public Guid Id { get; set; }
        public string Name { get; set; } = string.Empty;
        public double Price { get; set; }
        public string Url{ get; set; } = string.Empty;
        public DateTimeOffset LastUpdated { get; set; }
}

I am using the linq extension method from the Raven.Client.Documents namespace. Their official docs also show the same setup for querying an object : Docs. Unsure what the issue is here. Am I missing any configuration that I am not seeing?

0 Answers
Related