Entity Framework Core and sqlite: getting started on macOS and Visual Studio Code

Yassine Benabbas
4 min readMay 9, 2017

--

I couldn’t find a loge for Entity Framework Core :/

This article shows how to setup a multiplatform C# console project that uses Entity Framework Core (1.1.2) and SQLite. We will code on Visual Studio Code because it’s a 💪 IDE .

This article follows a code first approach. This means that the database columns, tables and data will be defined by code and will thus be created from scratch when the application runs. The other approach is called: database first. This one starts with an already existing database and generates code from it.

Please note that this post is about Entity Framework Core and not Entity Frameowrk 6.

Prerequisites:

Initializing the project:

Please follow these simple steps to get started

  • Start visual studio code (easy 👌)
  • Create an empty folder on your hard drive. This will be the location of our project.
  • Open that folder in visual studio code
  • Next, open a terminal (^` on mac). The working directory should be our project directory since it is the current folder on visual studio code. Otherwise, just “cd” to that folder using the terminal commands.
  • Run the following commands to start a new .Net Core project at the current location
  • “dotnet new console”; this will create a project file and a code file.
  • “dotnet restore”
  • After that, add the entity framework sqlite dependecy to your csproj file. You can use the vscode-nuget-package-manager extension to help you install it.
  • Run again “dotnew restore” to update the project with the new package.

We are all set up now. Let’s write some code 😀

Let’s code some:

Okay, we are doing great. We are ready to start writing a code first Entity Framework Core application.

Let’s develop an application that allows me to store in a SQLite database my video game collection (so that I won’t buy the same game twice 😅). In order to achieve that, we will perform the following tasks:

  • Create a C# class that represents a video game
  • Crete a class that inherits from DbContext which represent a database session.
  • Write CRUD code

Starting with the VideoGame class, we are going to define it like this:

Right click on the folder view to quickly create a Class

You are free to add colums and annotations: https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Next, we will define a DbContext sublass. Its roles are to specify the path of the sqlite file and that we are mapping the VideoGame class to a table. Here is the code allowing to achieve this:

Finally, we will add some CRUD code. For the sake of simplicity, we’ll write the CRUD code in the Main method. Here the commented code. Please read the comments for more information:

You should see the following text on the output view:

You can check that sqlite file is created in the bin/netcoreapp folder (which is the default working directory).

video_games.sqlite is in the middle
DB Browser for SQLite allows to check the content of the database

We have successfully created a console app without writing a single SQL command. Of course, we can write SQL commands if we want or if you like them 😍. In addition, you can read more samples here:

Conclusion

I hope that this article has illustrated how to quickly and easily setup a multiplatform Visual Code Project that creates and manipulates an SQLite database using code first Entity Framework Core.

Entity Framework Core offers many more features and we just scratched some of them.

Happy coding 😎

Links:

--

--

Yassine Benabbas

Mobile app developer at Worldline, teacher and PhD. I love programming, videos games and manga. Trying hard to learn some Japanese.