The repository was created as a demonstration of working with various database providers using ORM Entity Framework Core
Learn more about supported database providers for Entity Framework Core
-
After opening terminal (command line), install dotnet ef tool (if it is not already installed) by entering the following command:
dotnet tool install --global dotnet-ef
-
Make sure that correct connection string is entered in the appsettings.json file (learn more about connection strings)
-
From terminal (command line) go to samples directory
-
Command to add migration:
dotnet ef migrations add `Name` --project `Project` --startup-project `StartupProject` --context `Context`
- Command to update a database:
dotnet ef database update --project `Project` --startup-project `StartupProject` --context `Context`
- Command to remove migration:
dotnet ef migrations remove --project `Project` --startup-project `StartupProject` --context `Context`
Learn more about dotnet ef commands and parameters
Name
- The name of the migration.
For example: Initial
Project
- Relative path to the project folder of the target project. Default value is the current folder.
For example: EFCoreSample.Persistence.Sqlite
StartupProject
- Relative path to the project folder of the startup project. Default value is the current folder.
For example: EFCoreSample.Api
Context
- The name of the DbContext class to generate. If the DbContext is only one, then the attribute is not required.
For example: ApplicationDbContext
All connection strings must be located in the appsettings.json file (learn more about connection strings)
Connection string examples:
- MySQL
server=localhost;database=EFCoreSample;user=user;password=password
- Oracle
User Id=user;Password=password;Data Source=localhost:5432/EFCoreSample
- PostgreSQL
Host=localhost;Port=5432;Database=EFCoreSample;Username=postgres;Password=password
- SQLite
Data Source=EFCoreSample.db
- MS SQL Server
Server=(localdb)\\\\mssqllocaldb;Database=EFCoreSample;Trusted_Connection=True;
more MS SQL Server examples here
- MS Access
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=EFCoreSample.accdb;Persist Security Info=False
In this example, the EFCoreSample.Api project is connected to all database providers at once, which is basically impossible in a real project:
one project cannot contain multiple providers, the code given is just an EXAMPLE of how to initialize a context using different providers