Minimal API | In-memory veritabanı ile CRUD işlemleri | 1. Kısım

Minimal API ile in-memory veritabanı ile CRUD işlemlerinden listeleme ve ekleme işlemlerine baktık.

Github reposu: https://github.com/sonergonul/MinimalAPI

Bunun için Nuget içerisindeki Microsoft.EntityFrameworkCore.InMemory paketini kurduk öncelikle. Bu size in-memory olarak bir veritabanı sağlıyor. Daha sonra entity framework içerinde nasıl DbContext‘ten bir sınıf türetip içerisinde DbSet‘ler kullanarak tablolar oluşturuyorsak burada da aynı yapıyı kullanıyoruz. Sonrası aşağıdaki gibi;

Listeleme:

app.MapGet("/customers", async (CustomerDb db) => await db.Customers.ToListAsync());

Ekleme:

app.MapPost("/customers", async (CustomerDb db, Customer cus) => 
{
    await db.Customers.AddAsync(cus);
    await db.SaveChangesAsync();
    return Results.Created($"/customers/{cus.Id}", cus);
});

ID bazlı listeleme:

app.MapGet("/customers/{id}", async (CustomerDb db, int id) => await db.Customers.FindAsync(id));

Kaynaklar:

https://docs.microsoft.com/en-us/aspnet/core/tutorials/min-web-api?view=aspnetcore-6.0&tabs=visual-studio

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-6.0

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.