SQLite .NET Core 3.1 Console App

.NET Core 3.1 Console Application using SQLite with Entity Framework and Dependency Injection

This example shows how to incorporate ASP.NET concepts such as dependency injection within a console application using VS Code on Mac OS X / macOS or linux targets.

vscode

Project Structure

This solution is divided into three projects:

  • SqliteConsole: The main console application
  • SqliteConsole.Core: Entity models
  • SqliteConsole.Infrasture: Entity framework database context and service classes

Concepts

The following concepts are demonstrated within this example console application project:

  • SQLite Entity Framework
  • Dependency Injection

SQLite Entity Framework

Using dependency injection, the database context can be passed to a constructor of a class:

public class ExampleService : IExampleService
{
    private readonly SqliteConsoleContext context;

    public ExampleService(SqliteConsoleContext sqliteConsoleContext)
    {
        context = sqliteConsoleContext;
    }

This way, the context may be used as follows:

    public void GetExamples()
    {
        var examples = context.Examples
            .OrderBy(e => e.Name)
            .ToList();

Otherwise, there's a factory method to instantiate new contexts:

    using (var context = SqliteConsoleContextFactory.Create(config.GetConnectionString("DefaultConnection")))
    {
        var examples = context.Examples
            .OrderBy(e => e.Name)
            .ToList();
    }

Dependency Injection

Service classes are added to the main console application's Program.cs:

// Services
var services = new ServiceCollection()
    .AddSingleton<IExampleService, ExampleService>()
    .BuildServiceProvider();

Then, obtain the instance of the service as:

var service = services.GetService<IExampleService>();

Sqlite Dotnet Core

.NET Core 2.1 Console Application using SQLite with Entity Framework and Dependency Injection

Sqlite Dotnet Core Info

⭐ Stars 15
🔗 Source Code github.com
🕒 Last Update a year ago
🕒 Created 3 years ago
🐞 Open Issues 3
➗ Star-Issue Ratio 5
😎 Author jasonsturges