Entity Framework (EF) Core is a lightweight, extensible, open source and cross-platform version of Entity Framework data access technology. It’s rewritten version of Entity Framework Object-Relation-Mapper for .NET Core. It’s under .NET Foundation , hosted on GitHub, mainly developed by Microsoft, but totally open source. On short, you can use it everywhere: commercial apps, open source, you can propose changes and contribute to the development.
Permissions and licensing are under very very open and friendly .NET Core philosophy!!!!
EF enables two paradigms: Code-First and Database-First paradigm.
In this blog post I will show latter option. I will show how to scaffold or extract classes from existing relation database and us it in C# project.
Code-Fist approach means that you defined domain objects with all relations in your application and then database is generated based on your classes and relation between them. This option is generally better approach for greenfield projects – when you do your app from start.
Database-First approach means that you already have database and you need to generate domain object for your app. This approach is particular useful for developers who are more database-centric, or if database already exists (e.g. legacy systems) and we just need app to talk to this database.
As said, In this blog post I will show how to generate domain object from relation database and then later on use it in your app. For this, I will use only dotnet tool and console – no Visual Studio or some other fancy tools.
Scaffolding database structure into C# classes
Basic prerequisite is to install .NET Core 3.1 SDK. When you install this SDK you will get dotnet CLI tool. You can find more info about the tool here: https://docs.microsoft.com/en-us/dotnet/core/tools/.
For more Visual Studio type of developers: you can do everything with this. You can build, create project, add references to projects and a lot more. Furthermore, console based approach is very useful when you want to automatize some things. Therefore, I will use only console to do the job.
When I install this SDK I have dotnet tool available on my system.
You an find SDKs for different targets and platforms here: https://dotnet.microsoft.com/download/dotnet-core/3.1
I can check which tools are registered in dot net core tool repository, by using dotnet list command:
First, I need to install EF tool and register it globally.
Now, that I have EF tool available in my arsenal…
I can start my scaffolding process.
First, I create empty folder.
Inside I create empty class library class to put my scaffolded classes in.
Output: csproj with dummy Class1 class.
EF needs two NuGet references in order to work:
- Microsoft.EntityFrameworkCore.Design
- Microsoft.EntityFrameworkCore.SqlServer
So I put them in in my project. Of course via my dotnet tool!
Now, that everything is on place, the magic begins. I will scaffold my classes from existing SQL Server database. Let’s try
Models folder is created with all database mapped C# classes.
Let’s check our database structure quickly
and compare with scaffolded C# classes:
Everything is on place, classes in my C# project and DBContext class generated and ready to use in my application.
Command summary
Let’s quickly list commands I used for EF Database-First approach to generate my C# data classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
dotnet -- version dotnet tool list --global dotnet tool install dotnet-ef --global dotnet new classlib -f netcoreapp3.1 dotnet add <project-name>.csproj package Microsoft.EntityFrameworkCore.Design dotnet add <project-name>.csproj package Microsoft.EntityFrameworkCore.SqlServer dotnet ef dbcontext scaffold "Server=.;Database=BikeStores2;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models --project <project-name>.csproj |
Conclusion
If you are doing greenfield project you will most probably generate database from your code. In this case, you will used Code-First paradigm. But this approach is not necessary the best option.
Some developers (especially old-school ones 🙂 ) are more database-centic, so they are very comfortable with SQL/relational databases. In this case, you can use database first approach to use EF ORM mapping.
In this post I presented latter option. I presented how you can do EF Object-Relational mapping from existing database, generate business classes, DbContext and use it in your app. I did this by using dotnet tool and console.
Until the next time, happy coding.