Ad image

.Net Core EF ile code first migration

ahmetakdel
By
2 Min Read

.Net Core EF ile code first migration işlemi nasıl yapılır?

.Net Core EF ile code first migration lerine ait kodlar:

.Net Core 3.0 ile birlikte bazı değişiklikler oldu.

Visual Studio – Package Manager Console dan dotnet-ef paketini global olarak install edelim.

dotnet tool install --global dotnet-ef

Eğer EF CORE Tools update etmek istersek (The EF Core tools version ‘3.1.2’ is older than that of the runtime ‘3.1.3’. Update the tools for the latest features and bug fixes.)

dotnet tool update --global dotnet-ef

Daha sonra Microsoft.EntityFrameworkCore.Design son sürümünü install edelim:

dotnet add package Microsoft.EntityFrameworkCore.Design

Modellerimizdeki değişikliklerin database e yansıtılacak olan yeni migration dosyasının auto-build olması için şu kodu çalıştıralım:

Console:
dotnet ef migrations add InitialCreate
Shell:
Add-Migration InitialCreate

Projede farklı bir klasöre Migrations dosyalarını create edilmesini istiyorsanız:

Console:
dotnet ef migrations add InitialCreate -o Data/Migrations
Shell:
Add-Migration InitialCreate -o Data/SubFolder

Eğer birden fazla context varsa:

dotnet ef migrations add InitialCreate --context DataContext -o Mssql/Migrations

Değişiklikleri database e uygulamak için aşağıdaki kodu çalıştırabilirsiniz:

Console:
dotnet ef database update
Shell:
Update-Database

Migration dosyasını geri almak için bu kodu çalıştırabilirsiniz. Not: update den sonra işe yaramıyor.

Console:
dotnet ef migrations remove
Shell:
Remove-Migration

Generate SQL scripts

Console:
dotnet ef migrations script
Shell:
Script-Migration

Apply migrations at runtime

myDbContext.Database.Migrate();

Daha önce kodlama ile ilgili yazdığımız Sveltejs Nedir yazımıza burdan göz atabilirsiniz.

Share This Article
Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *