[英]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.MapGet
, app.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.