Simple example of Entity Framework Core Code-First approach.
Nuget Packages:
Microsoft.EntityFrameworkCoreMicrosoft.EntityFrameworkCore.ToolsMicrosoft.EntityFrameworkCore.SqlServerNpgsql.EntityFrameworkCore.PostgreSQL
Settings:
- Environmental variable:
SQLSERVER_MOVIES_LOCAL_CONNSTR - Environmental variable:
POSTGRES_MOVIES_LOCAL_CONNSTR
Postgres:
dotnet-ef migrations add PostgreSqlInitialMigration --project CodeFirst.DataAccess --context PostgresDbContextdotnet-ef database update --project CodeFirst.DataAccess --context PostgresDbContextdotnet-ef database drop --project CodeFirst.DataAccess --context PostgresDbContext
MS SQL Server:
dotnet-ef migrations add SqlServerInitialMigration --project CodeFirst.DataAccess --context SqlServerDbContextdotnet-ef database update --project CodeFirst.DataAccess --context SqlServerDbContextdotnet-ef database drop --project CodeFirst.DataAccess --context PostgresDbContext
-
Create a separate project
Models, where data models are stored. -
Create a separate project
DataAccess, that is responsible for interaction with database. -
Load packages to
DataAccessproject. See section Required packages. -
Set proper environmental variable for connection string for SQL Server database. For localhost it is:
"Data Source=DESKTOP-RN0NICT;Initial Catalog=MoviesCodeFirst;Integrated Security=true;" -
Set proper environmental variable for connection string for Postgre SQL database. For localhost it is:
"Server=localhost;User Id=postgres;Password=postgres;Database=MoviesCodeFirst;" -
Build a relations between models, for instance,
one-to-one,one-to-many,many-to-manyaccording to the database diagram. Refer to -
Create proper configurations of models, using Fluent API. Configuration should implement
IEntityTypeConfiguration<T>interface from Entity Framework namespace -
Create
BaseContextinherrit it fromDbContextin Entity Framework namespace, add related setsBDSet<T>, whereT-- models. -
Create
SqlServerContext, inherrit it fromBaseContextand override methodsOnConfiguringandOnModelCreating. -
Create
PostgreSqlContext, inherrit it fromBaseContextand override methodsOnConfiguringandOnModelCreating. -
Create migration for
SqlServerContext, use CLI command:dotnet-ef migrations add SqlServerInitialMigration --project CodeFirst.DataAccess --context SqlServerDbContext -
Create migration for
PostgreSqlContext, use CLI command:dotnet-ef migrations add PostgreSqlInitialMigration --project CodeFirst.DataAccess --context PostgresDbContext -
Update SQL Server database, use CLI command:
dotnet-ef database update --project CodeFirst.DataAccess --context SqlServerDbContext -
Update Postgre SQL database, use CLI command:
dotnet-ef database update --project CodeFirst.DataAccess --context PostgresDbContext -
Write unit tests for
SqlServerDbContext -
Write unit tests for
PostgresDbContext -
Create Generic repository interface
-
Implement Generic repository interface
-
Test Generic Repositories
To get MS SQL connection string proceed:
- Go to visual studio - Server explorer - Data connections
- Right click on data connections - Add new connection
- Type there your server name
- Click add connection
- Then in properties of such connection you'd get connection string