簡體   English   中英

EF Core InMemory DB 不應用程序集中的配置並且不返回任何數據

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

我正在嘗試使用 InMemory DB 測試我的應用程序,以便可以針對它運行 Postman 測試。

我有一個成功啟動它的docker-compose

version: '3.9'

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

為 InMemory DB 設置了測試環境:

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

我在這里成功配置了數據庫服務:

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
            {
                ...
            }
        }
    }
}

我得到這樣的成功日志:

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

請注意在OnModelCreating()中設置的“Ended Seeding and Applying”的日志

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

我所有的配置種子數據都是這樣的:

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"
                }
            );
        }
    }
}

但是當我訪問http://localhost/api/Tags下的集合時,我得到[] 我可以使用 REST 客戶端創建新資源並讀取它們,但我希望應用我的種子數據。 為什么配置不應用來自builder.HasData()的值?

要真正進行播種,您需要調用:

context.Database.EnsureCreated();

嘗試:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM