This repository is a playground for Entity Framework Core. It contains a simple console application that demonstrates how to use EF Core to interact with a SQLServer database.
In this project, I try to practice the following concepts:
- How to use EF Core with SQLServer, in DB First approach. (Look at this project: EFPlayground/DbFirst)
- How to use EF Core with SQLServer, in Code First approach.
- Using Data Annotations (Look at this project: EFPlayground/CFDataAnnotations
- Using Fluent API (Look at this project: EFPlayground/CFFluentAPI)
Look at the database schema using DBML
file here
Note
There is another approach for this design to create parent table for Student
and Instructor
tables, and use Discriminator
column to differentiate between them. But I wanted to keep it simple and separate them.
-
Clone this repository
git clone <repo_url>
-
Open the solution file in Visual Studio
-
In
Config
Project, Copyapp.settings.example.json
and Createapp.settings.json
file in the same directory and fill in the connection string. -
Restore the NuGet packages, by right-clicking on the solution and selecting
Restore NuGet Packages
. -
Build the solution.
-
Run the console application project you want to run.
Note
if you want to run the DbFirst
project, you need to create a database first (using the scripts provided in the scripts
directory) and update the connection string in the app.settings.json
file and then run scaffold-dbcontext
command to generate the models.
EFPlayground
├── EFPlayground.sln
├── Config
│ ├── app.settings.example.json
│ ├── app.settings.json
├── DbFirst
│ ├── Program.cs
│ ├── Models
│ │ ├── SchoolContext.cs # (Add by EF Core)
│ │ ├── SchoolContext.Developer.cs #(Added by me to add custom methods)
│ │ ├── Student.cs #(Added by EF Core)
│ │ ├── Student.Developer.cs #(Added by me to add custom methods)
├── CFDataAnnotations
│ ├── Program.cs
│ ├── Models
│ │ ├── Student.cs
│ ├── Database
│ │ ├── SchoolDbContext.cs
├── CFFluentAPI
│ ├── Program.cs
│ ├── ModelConfigurations
│ │ ├── StudentConfiguration.cs
│ ├── Models
│ │ ├── Student.cs
│ ├── Database
│ │ ├── SchoolDbContext.cs
├── docs
│ ├── database.dbml
│ ├── erd.png
├── scripts
│ ├── create_db.sql
│ ├── populate_db.sql
├── .gitignore
├── README.md
This repository is licensed under the MIT License - see the LICENSE file for details.