Simple example of Entity Framework Core Code-First approach.
Nuget Packages:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.SqlServer
Npgsql.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 PostgresDbContext
dotnet-ef database update --project CodeFirst.DataAccess --context PostgresDbContext
dotnet-ef database drop --project CodeFirst.DataAccess --context PostgresDbContext
MS SQL Server:
dotnet-ef migrations add SqlServerInitialMigration --project CodeFirst.DataAccess --context SqlServerDbContext
dotnet-ef database update --project CodeFirst.DataAccess --context SqlServerDbContext
dotnet-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
DataAccess
project. 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-many
according 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
BaseContext
inherrit it fromDbContext
in Entity Framework namespace, add related setsBDSet<T>
, whereT
-- models. -
Create
SqlServerContext
, inherrit it fromBaseContext
and override methodsOnConfiguring
andOnModelCreating
. -
Create
PostgreSqlContext
, inherrit it fromBaseContext
and override methodsOnConfiguring
andOnModelCreating
. -
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