简体   繁体   English

最小的 Web API 和播种内存中实体框架数据库

[英]Minimal Web API and seeding an in-memory Entity Framework database

I successfully created a minimal C# ASP.NET Core 6.0 Web API project in Visual Studio 2022 using Microsoft's minimal Web API tutorial located here . I successfully created a minimal C# ASP.NET Core 6.0 Web API project in Visual Studio 2022 using Microsoft's minimal Web API tutorial located here .

In this tutorial, the author uses an in-memory Entity Framework database.在本教程中,作者使用内存中的实体框架数据库。 I would like to seed the database with fake data when the program starts.我想在程序启动时用假数据播种数据库。

I'm not sure where or how I can access my DbContext to seed initial data in the database.我不确定在哪里或如何访问我的DbContext以在数据库中播种初始数据。 The author states, "The DI container provides access to the database context and other services."作者指出,“DI 容器提供对数据库上下文和其他服务的访问。” This is correct, but I would like to access the DbContext outside the scope of the Map methods ( app.MapGet , app.MapPost , etc.)这是正确的,但我想访问 Map 方法( app.MapGetapp.MapPost等)的DbContext之外的 DbContext

How can I do something like this when the application starts?当应用程序启动时,我该如何做这样的事情?

db.ToDoEntities.AddRange(new List<ToDoEntity>
{
    new ToDoEntity
    {
        Name = "Code",
        IsComplete = false
    },
    new ToDoEntity
    {
        Name = "Game",
        IsComplete = false
    },
    new ToDoEntity
    {
        Name = "Sleep",
        IsComplete = false
    }
});

db.SaveChanges();

Here is the code I have so far:这是我到目前为止的代码:

// ToDoDbContext.cs
using Microsoft.EntityFrameworkCore;

namespace ToDo.Api;

public class ToDoDbContext : DbContext
{
    public ToDoDbContext(DbContextOptions<ToDoDbContext> options) : base(options) { }
    public DbSet<ToDoEntity> ToDoEntities => Set<ToDoEntity>();
}

// ToDoEntity.cs
namespace ToDo.Api;

public class ToDoEntity
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public bool IsComplete { get; set; }
}

// Program.cs
using Microsoft.EntityFrameworkCore;
using ToDo.Api;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddDbContext<ToDoDbContext>(options => options.UseInMemoryDatabase("ToDoList"));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

var app = builder.Build();

// Example GET end-point.
app.MapGet("/to-do-items", async (ToDoDbContext db) => await db.ToDoEntities.ToListAsync());

// All other map operations here. See the tutorial for the full code.

app.Run();

Go to ToDoDbContext and add this method Go 到 ToDoDbContext 并添加此方法

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    //Example
    modelBuilder.Entity<ToDoEntity>().HasData(
    new ToDoEntity{ id= 1, Name = "First", Code = false },
    new ToDoEntity{ id= 2, Name = "First", Code = false ​});
}

then run add-migration through your vs studio terminal然后通过你的 vs studio 终端运行 add-migration
which will generate a migration based on your data seed then run update-database这将根据您的数据种子生成迁移,然后运行 update-database

reference Visit https://docs.microsoft.com/en-us/ef/core/modeling/data-seeding参考访问https://docs.microsoft.com/en-us/ef/core/modeling/data-seeding

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架5.0,代码优先,关系,流利的API和播种数据库 - Entity Framework 5.0, Code First, relationships, fluent API and seeding the database 如何实现数据库播种以使用 Entity Framework 进行测试 - How to implement database seeding for testing with Entity Framework Entity Framework 7 创建 DbSet 的内存实现 - Entity Framework 7 Create in-memory implementation of DbSet ASP.NET 核心 Web API - 使用实体框架在 .NET 核心 6 中播种数据 - ASP.NET Core Web API - seeding data in .NET Core 6 using Entity Framework C#实体框架内存数据库如何在单元测试中跳过或测试标量函数 - C# Entity Framework in-memory database how to skip or test the scalar function in unit test 实体框架不播种数据 - Entity Framework not seeding data 实体框架6-播种数据库引发System.NullReferenceException - entity framework 6 - seeding database throws System.NullReferenceException 使用Entity Framework Core以多对多关系为数据库播种 - Seeding the database with a many-to-many relationship using Entity Framework Core 内存中实体框架 HasComputedColumnSql 与单元测试内部的 function 复制 - In-memory Entity Framework HasComputedColumnSql with the function replicate inside of unit test ASP.NET Identity 3.0实体框架数据库种子 - ASP.NET Identity 3.0 Entity Framework Database Seeding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM