This repository contains a sample code project that demonstrates how to use the Entity Framework Core library to connect to and query data from a SQL Server (or Azure SQL) database using C#.
The code in this repository connects to localdb
, uses a script to create a new database and uses EF Core to read from its data. The code demonstrates how to map database entities to C# classes and use the C# classes in place of the database entities. The repository also shows the same functionality implemented in Dapper and ADO.NET, to demonstrate the amount of operations and tasks that is abstracted by EF Core.
This sample draws from this tutorial for Getting Started with EF Core with SQLite.
- .NET Core 6.0 or higher
- SQL Server Express
localdb
, which can be installed as an option during SQL installation
- SQL Server Management Studio (SSMS)
- Visual Studio
- A basic understanding of C# and SQL
- Clone or download the repository to your local machine.
- Use Visual Studio to debug and run the project.
The file GenerateSampleDatabase.sql
will generate a SQL Server database in (localdb)\mssqllocaldb
under the master
database and insert a single blog post. The sample code samples depend on having an existing database to connect to and query.
Each code sample has the same functionality implemented in a different way. When run, the code will generate a console app that connects to a SQL Server (or Azure SQL database with the appropriate connection string) and queries it for all the blog posts stored.
If you wish to contribute, you can submit a pull request.
Overall, this code is intended to provide a basic understanding of how Entity Framework Core can be used to interact with a SQL database in a C# application, and should not be used as-is in a production environment. Instead, this code should be used as the beginning passage to build your database application.
Please let me know if you need more help by creating an issue.