Skip to content

Entity Framework - Sql Server DB

This component uses Entity Framework to read and write data to and from an Sql Server DB instance.

It can be used in Read and Write pipelines.

Configuration

solutionName: "Axellon.SimpleSql.Ex"
dataApi:
  entityGroups:
    - group: default
      enableHistory: false
      useGroupFolder: true
      readComponents:
        - type: EFSqlComponent
      writeComponents:
        - type: ValidationComponent
        - type: EFSqlComponent
      endpoints:
        - type: WebApiEndpoint

Connection string

The connection string that is used in the generated solution can be defined in:

  • the environment variable ConnectionStrings__SqlConnectionString,
  • appsettings.json file or
  • other sources of configuration values defined in the application.

The name of the config value can be changed in the generated repository project in SharedServiceRegistration.cs file.

Code details

Among others, the generated code will contain these classes:

  • EntityContext

This class initializes DBContext, sets the connection string and defines the properties and constraints of the entity. * EntityContextDesignTimeFactory

Used mainly by the dotnet ef tool for creating migration scripts and initializing the database.

Migration scripts

To work with migrations, EF Core CLI tool is required.

dotnet tool install -g dotnet-ef --interactive

To create the initial migration file in Migrations folder, use the following command

 dotnet ef migrations add Initial_migration --output-dir "<path to your repository project>\Migrations" --project "<path to your repository project>\<project file name>.csproj" -- --cs "<connection string to your database>"

To update the database, you can use:

dotnet ef database update --project "<full path of csproj with dbcontext>" -- --cs "<sql server connection string>"
Back to top