Chapter 4. Working with a Database
There are many options when it comes to working with a database. You can choose anything from SQL Server, Oracle, MySQL, or even a Microsoft Access database! My personal preference is SQL Server. I don’t claim it to be the best; however, I do think it is the best choice when working with MVC. ASP.NET and the MVC framework are built and maintained by Microsoft. Microsoft is also the owner of SQL Server. Because of this, I think it provides the best support.
Just like databases, there are a variety of ways to integrate a database into your MVC application. You can write your own SQL statements and access the database by using the classes under the SqlClient namespace. Or you can use an ORM (Object-Relational-Mapper) that wraps your database access.
What Is an ORM?
An ORM converts a database table into a model, which allows you to use it like any other class in a project. For example, in Chapter 3, a Person class was created. With an ORM, this class could be mapped to a Person table.
When you fetch the object from your database, the ORM would return the Person class populated with the data. Likewise, saving data to the database would involve creating a new Person object populated with the data to save.
My preference is the latter. A framework like Entity Framework (EF) makes it easy to create, access, and maintain your database with an MVC project.
There are several other frameworks like EF, such as NHibernate; however, like SQL Server, Entity ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access