
This short tutorial is to give a quick rundown of using SQLite with C# in. This is the Index.cshtml file = "Home ( > (var song in songs Public IndexModel(ILogger logger, SongRepository repository) Private readonly SongRepository _repository Now that the repository is ready and part of the dependency injection you can add it to your controller or page’s contstructor and it will be made available by the background magic.

Services.AddSingleton(typeof(SongRepository), new SongRepository(Configuration.GetConnectionString("Database"))) Connecting to the DatabaseĪfter adding the connection string to your appsettings.json you can add your repository as a singleton service in startup.cs public void ConfigureServices(IServiceCollection services) Net Core projects compile might seem misleading about just where to put your database. The connection string has some options that might be useful in different scenarios but the basic string to use is to specify the path or just the filename if the database will be in the directory next to the running assembly Data Source=Database.db Just remember to click Write Changes before closing the program! DB Browser for SQLite is great for working with SQLite databases and has a good, clean, interface with all the tools you need right where you need them. But sometimes it’s useful to have a more visual approach, especially when you’re getting to grips with how it all works. db extension which works fine if you’re going to generate your database from code. With SQLite it’s actually pretty easy to make the database: Create a blank file and give it a. This is not the approach to use in production but it fine for building an example to show an idea.

Throw new ArgumentException(nameof(sqliteConnectionString)) If (string.IsNullOrWhiteSpace(sqliteConnectionString))

Public SongRepository(string sqliteConnectionString) This keeps all your data work in one place which is handy if you later decide to move it out to a separate project or even refactor it to work a different way. You will need to create a class of any name you choose and decide that all ‘data’ work happens within the class, so the rest of your project does not know of or use SQLite in any way. You could add other packages like Entity Framework if any others are suitable to your project.įor quickly putting something together to test an idea I like to start off with the repository pattern which can be a very simple way of dealing with data. You now have the ability to use SQLite in your web project.
#Visual basic collections sqlite tutorial install
Go to Browse, search for and install the package.Right click your project and select Manage NuGet Packages.
