alaawahbah
11/28/2018 - 7:17 AM

Entity Framework Core Command

    Scaffold-DbContext "Server=.\SQLEXPRESS;Database=SOSample;User ID=sa;Password=P@ssw0rd;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Branch -Project circle.EF -Context BranchContext -Tables items,department,transaction,transactionentry

--------------------------------------
Scaffold-DbContext "Server=192.168.7.114; Database=nerp-ksa;  User=naseej; Password=P@ssw0rd;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Project CommonTestNg.EntityFrameworkCore -Context CommonTestNgDbContext -Schemas comn

---------------------------------------------

Add-Migration init2 -StartupProject CreativeTicketing.Core -Context CreativeTicketing.Core.Context.CreativeTicketingContext -OutputDir Migrations/TicketMigrations

Add-Migration init2  -Context CreativeTicketing.Core.Context.CreativeTicketingContext -OutputDir Migrations/TicketMigrations
Update-Database

How to migrate Db on production?

Using migrate in our code

using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()

.CreateScope())

{

serviceScope.ServiceProvider.GetService<RegistryDbContext>()

.Database.Migrate();

}

It will implement in our code of api in startup code it will check for Migrate DB every time application startup

Use it in exe app or in Api project

use Database Initializer

https://www.codeproject.com/Tips/814618/Use-of-Database-SetInitializer-method-in-Code-Firs

I don't prefer because it good for development for small app or test not enterprise

Using ef core tool

Install:

dotnettool install --global dotnet-ef

and run tools on published version of your app

dotnet ef migrations script --idempotent --output "script.sql" --context MyDbContext

dotnet ef migrations script --idempotent (if you don't know what version the database is at)

or dotnet ef migrations script <from-migration> (to get the changes after from-migration)

dotnet ef database update --startup-project startupAssembly --project migrationsAssembly or dotnet ef migrations script --startup-project startupAssembly --project migrationsAssembly - migrations.sql

this commands create sql file for change it will applied on your db.