What is Entity Framework Core (EF Core)?
You probably have already heard of Entity Framework before, perhaps in the context of plain ASP.NET MVC where it was also often used. Entity Framework Core is really the core, let’s say the next version of Entity Framework, the ORM, or object-relational mapper, from Microsoft.
Entity Framework (EF) Core is a lightweight, extensible, open source and cross-platform version of the popular Entity Framework data access technology.
- EF Core is a cross-platform software making it agile to be functional on different operating systems like Windows, Mac OS, and Linux OS.
- EF Core serves as an object-relational mapper (ORM), enabling .NET developers to work with a database using .NET objects, and eliminating the need for most of the data-access code they usually need to write.
- Entity Framework Core performs CRUD operation automatically without the need of writing all the SQL queries manually every-time
- Earlier developers used the Database First approach but now in the Entity Framework Core, the Code First approach is used to generate Model using the existing database
Let’s dig deeper by understanding the Database-First approach and the Code-First approach
In the Database-First approach, database and tables are first created, then the entity Data Model is generated using the created database. If you already have a database designed, then you can go with this approach. You can modify the database manually and update the model from the database. Data mapping and creation of keys and relationships are easy as you do not have to write any code for it. This approach is useful for data intense and large applications. Not to mention this has been a preferred approach for development work for a long time now.
The major disadvantage of this approach is the large pile of auto-generated code that gets created while generating the .edmx model file and the associated code models. Manually updating the database can be tricky if we’re dealing with clusters, multiple instances, or a number of environments (development, testing, production etc). Additionally, we will have to manually keep them in sync instead of relying upon code-driven updates/migrations or autogenerated SQL scripts. Lastly, maintaining the database code in source control is challenging.
The Code-First approach allows the developer to define model objects using only standard classes, without the need of any design tool, XML mapping files, or cumbersome piles of autogenerated code. In other words, we can say that, going Code First means writing the Data Model entity classes we’ll be using within our project and let Entity Framework generate the Database accordingly. This has been the Entity Framework flagship approach since EF4, which enables an elegant, highly-efficient Data Model development workflow.
Code-First workflow begins with classes that describe the conceptual model and then Entity Framework generate a database from that model automatically. Code-First approach offers most control over the final appearance of the application code and the resulting database.
Why Code-First Over Database-First?
Let me make one thing clear from the get go – compared to the previous versions of Entity Framework, EF Core only supports a Code-First approach or rather Microsoft recommends a code-first approach. The database-first approach isn’t supported by EF Core any longer. This doesn’t mean, that you cannot have the model be generated from the database. That is still supported. What isn’t supported is the EDMX-based approach. If you’re still a bit unclear on the role and goal of an ORM such as EF Core, let me explain in a bit more detail.
Keep in mind Microsoft is releasing .NET 5 (no ‘core’ no ‘framework’) later this year which is the next update to .NET Core 3.1. My previous post talks about .NET 5.
In order to work with the data in a database, we can write SQL statements in our code or call a stored procedure that resides in the database. While that approach has worked for many, many years, an ORM can make the life of a developer so much easier. Developers typically like to work with classes and objects. That’s where EF Core comes into the picture. The ORM really sits between the code and the database. And it will map the objects we have in code with data in the database. This enables us as developers to focus on the code part and not so much on the database side of things.
Advantages of Code-First Approach
Some of other advantages of Code-First approach over Database-First approach are –
- Version Control the Database Code – versioning databases has always been tricky. However with code first approach, this changes. Because the database schema is fully based on code models, by version controlling source code you’re versioning your database
- Faster Development – There is no dependency between developers! Easier and quicker to add a property to a class in the code compare to add a column to a table. Full control over database design
- Support for DevOps – Doesn’t require a separate resource to manage databases. Easy to generate Create, Update and Delete scripts. Much more support for test data and unit testing
In a few days I will post a tutorial on code-first approach while developing an ASP.NET using ASP.NET Core MVC 3.1 describing the process of creating database objects through code.
Till then, stay home and stay safe…