简体   繁体   中英

EF Core InMemory DB does not apply configurations from assembly and returns no data

I'm trying to test my app with an InMemory DB so I can run Postman tests against it.

I have a docker-compose that successfully starts it:

version: '3.9'

services:
  api:
    image: ${DOCKER_REGISTRY-}api-test
    build:
      context: ../
      dockerfile: API/Dockerfile
    ports:
        - 80:80
        - 443:443
    environment:
      - ASPNETCORE_ENVIRONMENT=Test

The test env is setup for the InMemory DB:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "UseOnlyInMemoryDatabase": true
}

I successfully configure the DB service in here:

namespace Infrastructure
{
    public static class Dependencies
    {
        public static void ConfigureServices(IConfiguration configuration, IServiceCollection services)
        {
            var useOnlyInMemoryDatabase = false;
            if (configuration["UseOnlyInMemoryDatabase"] != null)
            {
                useOnlyInMemoryDatabase = bool.Parse(configuration["UseOnlyInMemoryDatabase"]);
            }

            if (useOnlyInMemoryDatabase)
            {
                services.AddDbContext<BookDesinerContext>(c =>
                   c.UseInMemoryDatabase("BookDesignerDB"));
            }
            else
            {
                ...
            }
        }
    }
}

I get the successful log like this:

info: API[0]

      PublicApi App created...

info: API[0]

      Seeding Database...

Starting Seed Category

Ended Seeding and applying

warn: Microsoft.EntityFrameworkCore.Model.Validation[10620]

      The property 'GameCell.Settings' is a collection or enumeration type with a value converter but with no value comparer. Set a value comparer to ensure the collection/enumeration elements are compared correctly.

info: Microsoft.EntityFrameworkCore.Infrastructure[10403]

      Entity Framework Core 6.0.8 initialized 'BookDesinerContext' using provider 'Microsoft.EntityFrameworkCore.InMemory:6.0.7' with options: StoreName=BookDesignerDB 

info: API[0]

      LAUNCHING API

info: Microsoft.Hosting.Lifetime[14]

      Now listening on: http://[::]:80

Notice the log from "Ended Seeding and Applying", which is set in OnModelCreating()

// Seed
builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
Console.WriteLine("Ended Seeding and applying");

All my configs seed data like this:

namespace Infrastructure.Data.Seeding
{
    public class TagConfig : IEntityTypeConfiguration<Tag>
    {
        public void Configure(EntityTypeBuilder<Tag> builder)
        {
            builder.ToTable("Tag");
            builder.Property(t => t.Value).IsRequired();

            builder.HasData(
                new Tag
                {
                    TagId = 1,
                    Key = "Held",
                    Value = "Borja"
                },
                new Tag
                {
                    TagId = 2,
                    Key = "Genre",
                    Value = "Pirat"
                }
            );
        }
    }
}

But when I access the collection under http://localhost/api/Tags I get [] . I can use a REST client to create new resources and read them, but I want my seed data to be applied. Why does the config not apply the values from builder.HasData() ?

For the seeding to actually happen, you need a call to:

context.Database.EnsureCreated();

Try:

services.GetRequiredService<BookDesinerContext>().Database.EnsureCreated();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM