TOC

This article is currently in the process of being translated into Russian (~10% done).

Working with databases:

Introduction: Picking a database

Для большинства языков программирования и фреймворков, возможность взаимодействовать с базой данных, является очень важным аспектом. Еще больше это касается фреймворков нацеленных на работу с вебом, где вы быстро столкнетесь с необходимостью базы дынных, для хранения данных вашего веб сайта. Также можно использовать и другие решения для хранения данных, например, файлы в формате XML или JSON, но по сравнению с работой с хорошей базой дынных у них есть некоторые недостатки. Но на нашу удачу - ASP.NET MVC упрощает нам начало работы с базой данных.

Picking a database engine

So what is a good database? There are almost as many answers to that question as to the classic "What is the best programming language?"-question, but much like when you have to pick a programming language, the answer is usually "Pick the best one for the current task". And you're in luck, because the .NET framework, and thereby ASP.NET MVC, supports a huge selection of database engines, either natively or with the help of a database provider found in the NuGet package system.

On the other hand, when using ASP.NET MVC to create a website, the task is often very all-round, where your solution has to solve many different types of problems. Therefore, it often makes sense to pick a database engine that does a lot of things very well, like Microsoft SQL Server. It has a couple of major advantages:

  • It's made by Microsoft, just like the .NET framework, so communication between the two will always be optimal
  • If you choose to have your web solution hosted by one of the many hosting providers who support ASP.NET MVC Core, they will likely also offer SQL Server
  • You can easily, and for free, use the "SQL Server Express LocalDB" (more on that later) during the development process on your PC
  • You can work with the database (design tables, edit rows etc.) directly from Visual Studio
  • It scales very well - you can use it for a small, personal website, and if that website should grow into a huge community with thousands of visitors, SQL Server will still have more than enough power and scalability to support it

MS SQL Server is a great choice to get you started working with databases in ASP.NET Core MVC, which is why we will be using it in this tutorial. However, as soon as you have seen how ASP.NET MVC and MS SQL Server works together, feel free to experiment with other databases - you will soon realize that a lot of what you have just seen and learned can be re-used even if the underlying database changes.

What if you're not working on Windows?

A lot of .NET developers use Windows machine for the development process, since Visual Studio was historically only available on this OS. However, times are changing and Microsoft has been offering a very nice version of Visual Studio for Mac for several years. You can of course also write .NET code on a Linux computer, either using Visual Studio Code or any other text editor + the .NET compiler.

However, MS SQL Server is still a bit more limited when it comes to your choice of Operating System. As of writing, SQL Server (and the LocalDB implementation) will for instance not run on a Mac. This is not a problem if you have access to a remote SQL Server, running on Windows somewhere on the Internet, but if you want to be able to do local testing of your database, you may consider using a different database engine. A great alternative to MS SQL Server could be SQLite, which uses the SQL language just like MS SQL Server. In other words, you can use this alternative and still follow most of this tutorial.

Summary

To show you how to work with databases in ASP.NET MVC, I have decided to create a complete database-driven web application, using Microsoft SQL Server, from scratch. We'll be implementing a TODO-list and in the following articles I will take you through the entire process, where we'll be using a lot of the techniques already demonstrated in this tutorial, while combining them with the techniques needed to work with a database. As a little teaser, here's a screenshot of what we'll be building:

However, before we start setting up the database and writing some code, we need to talk about another important subject: Which database framework should we use for the communication between the your code and your database? We'll discuss that in the next article.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!