Entity Framework Core Nedir?
EF Core, .NET uygulamalarında veritabanı işlemlerini C# sınıfları üzerinden yapmanızı sağlayan ORM aracıdır. SQL Server, MySQL, PostgreSQL ve SQLite destekler.
DbContext Tanımlama
public class AppDbContext : DbContext {
public DbSet<Kullanici> Kullanicilar { get; set; }
public DbSet<Urun> Urunler { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder opt) {
opt.UseSqlServer("Server=.;Database=AppDb;Trusted_Connection=true");
}
}CRUD İşlemleri
using var ctx = new AppDbContext();
// Ekle
ctx.Kullanicilar.Add(new Kullanici { Ad = "Ali", Email = "ali@mail.com" });
ctx.SaveChanges();
// Oku
var kullanici = ctx.Kullanicilar.FirstOrDefault(k => k.Id == 1);
// Güncelle
kullanici.Ad = "Veli";
ctx.SaveChanges();
// Sil
ctx.Kullanicilar.Remove(kullanici);
ctx.SaveChanges();Migration
dotnet ef migrations add IlkMigrasyon
dotnet ef database update