Minimal API ile in-memory veritabanı ile CRUD işlemlerinden listeleme ve ekleme işlemlerine baktık.
#dotnet6 #minimalapi #crud
✅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) lambda await db.Customers.ToListAsync());
Ekleme:
app.MapPost("/customers", async (CustomerDb db, Customer cus) lambda
{
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) lambda await db.Customers.FindAsync(id));
Kaynaklar:
https://docs.microsoft.com/en-us/aspn...
https://docs.microsoft.com/en-us/aspn...
https://www.nuget.org/packages/Micros...
***
Sosyal Medya
🐦 Twitter'dan takip edin: / sonergonul
💜 Twitch'ten takip edin: / sonergonul
💚 Discord kanalımız: / discord
💖 Quora'dan takip edin: https://www.quora.com/profile/Soner-G...
💛 Instagram'dan takip edin: / sonergonul
Destek
💪 ByNoGame: https://www.bynogame.com/destekle/son...
💪 Stremlabs: https://streamlabs.com/sonergonul/tip