Skip to content

dkolzenov/efcore-samples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Entity Framework Core providers sample

★ Put a star if the repository was useful ★

The repository was created as a demonstration of working with various database providers using ORM Entity Framework Core

Providers

Learn more about supported database providers for Entity Framework Core

Configuring migrations

  1. 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

  2. Make sure that correct connection string is entered in the appsettings.json file (learn more about connection strings)

  3. From terminal (command line) go to samples directory

  4. Command to add migration:

dotnet ef migrations add `Name` --project `Project` --startup-project `StartupProject` --context `Context`
  1. Command to update a database:
dotnet ef database update --project `Project` --startup-project `StartupProject` --context `Context`
  1. Command to remove migration:
dotnet ef migrations remove --project `Project` --startup-project `StartupProject` --context `Context`

Learn more about dotnet ef commands and parameters

Reference for migration commands:

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

Connection strings

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

more MySQL examples here

  • Oracle
User Id=user;Password=password;Data Source=localhost:5432/EFCoreSample

more Oracle examples here

  • PostgreSQL
Host=localhost;Port=5432;Database=EFCoreSample;Username=postgres;Password=password

more PostgreSQL examples here

  • SQLite
Data Source=EFCoreSample.db

more SQLite examples here

  • 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

more MS Access examples here

Pay attention

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