Install db provider: Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install Tools: Install-Package Microsoft.EntityFrameworkCore.Tools
Create class DatabaseContext which derrives from DbContext
using Microsoft.EntityFrameworkCore;
using EF_Core_Demo.Models.DB;
namespace EF_Core_Demo.Models
{
public class DatabaseContext : DbContext
{
public DatabaseContext(DbContextOptions options) : base(options)
{
}
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
//public DbSet<Ratings> Ratings { get; set; }
}
}Create class Category and Product ...
Adjust method ConfigureServices of class Startup.cs
const string serverName = "LENOVO-LUCA";
const string dbName = "SampleDb";
var connection = @"Server="+ serverName + ";Database=" + dbName + ";Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext<DatabaseContext>(options => options.UseSqlServer(connection));-
Open the PMC
Tools –> NuGet Package Manager –> Package Manager Console -
Run
Add-Migration InitialSetup -
Run
Update-Databaseto apply the new migration to the database.
Q: Which connection string is used?
A: Somehow it uses the code (Solution -> Startup Project)
Q: How to revert a migration?
A: Use commandRemove-Migrationto revert the generated migrations