Getting Started with Entity Framework Core

In this article, you will learn about the new lightweight version of entity framework that is entity framework core. If you have entity framework experience, you may be aware that EF core has been written from scratch to achieve some new goals. Here you will learn about similarities and differences from EF 6.

So What is Entity Framework ?

Entity framework is the set of .net APIs for performing data access in your software  and entity framework  is official data access tooling  from Microsoft .

It originated from Microsoft research and  then it was adopted from team who was responsible for ADO.Net as the next generation of Microsoft’s data access technology .

EF has gone through number of major revolution with most of new capabilities being added on top of what was built into the .net framework.With EF6 ,Entity framework was moved to codeplex and became open source and since then it moved to github and remains quite active.

Entity framework fits into a category of data access technologies called Object Relational Mappers (ORM). ORMs are designed to reduce the friction between how data is structured in relational database and how you define your classes ,without ORM we have to transform lot of code to transform database results into instance of our classes inn our software .

An ORM allows us to express our queries using our classes and then ORM builds and executes relevant SQL for us,but Entity framework is different from any ORM you might be familiar with.

While a typical ORM infers that the classes and database tables are of  a similar structure,Entity framework has a mapping layer in between and gives us a lot more flexibility in to how to get from objects to table and from object properties to table columns and for these mapping entity framework does start with some assumptions and we refer to those as  conventions but then you can apply additional mappings rules on top of conventions to be sure that in your specific scenario that the data is able to find its way back and forth between your objects and your database.

 

Why Entity Framework ??

EF Core Advantages

Using an ORM can really eliminate a  slew of redundant data interaction tasks and doing so EF can enhance developer productivity.

Rather than writing relevant SQL to target whatever relational  database you work with , EF uses the LINQ syntax that’s part of  the .net framework. LINQ to entities allows developer to use consistent and strongly typed query language regardless of which database they are targeting additionally LINQ for objects is used for querying other elements of .net.

Using ORM allows developers to focus on their domain , their business rules and their business objects .They don’t have to worry about direct interaction with the database or being  intimately familiar with the databse schema.

What is Entity FrameWork Core ?

EF core was released in late June 2016 . Entity Framework Core is lightweight and extensible version of popular Entity Framework data access technology .This is not simply an update from EF6, its  a different kind of entity framework. Like EF6, EF core is open source and available on GitHub.

Where can you Use EF Core ?

Entity Frame work core runs on .NET core and .NET core runs in lot of places :
  • .NET 4.5.1+
  • .NET Core
  • UWP Universal Windows Platform for Windows 10 that runs o any device.
If you are targeting cross platform or  UWP you have to use EF Core.

EF6 features not coming to EF Core

  • No EDMX /Designer in EF Core
  • ObjectContext API
  • Entity SQL
  • Metadata Workspace API
  • Overly complex mapping
  • MEST* mapping

How EF Core Works?

EF Working

  • First you need your domain classes ,this part actually has nothing to do with EF,this is just defining your class.
  • Then you use EF APIs to define a data model based on those domain classes .You also use EF APIs to write and execute LINQ to entities Queries against those classes ,and in your code you need to call entity framework save changes to push back data to the database.
  • EF APIs keep track of the state of object that it’s aware ,it will determine the SQL it needs to save back to the database, and for queries ,EF will transform your LINQ to Entities Queries into SQL ,execute that SQL and then create objects from query results.
  • EF Manages all of the interaction with the data store .

Some developers are starting a new project from the scratch and will most likely start by planning how the domain will function and that will help them define the classes then they can code the classes and when it’s time to add in the data persistence you can then  create DbContext classes to define data models that wrap those domain classes , the data model can then be used as entity framework migrations to create and evolve the database.

But often we already have a  existing database  and while those database tables and views and stored procedure may not reflect what your domain classes should look like they do provide a good head start.

And its possible with EF core to start by reverse engineering one or more tables into DbContext class that is your data model and a set of domain classes and that uses  Scaffolding feature.

 

Mapping Classes & properties to data-base

When defining the data model, your classes aren’t required to exactly match the schema of the database , EF has default rules for how it will infer what database schema looks like.

Basic Work flow

EF WorkFlow

Starts with domain classes , you can then use EF DbContext API to wrap the classes into a model and instruct EF as to how those classes in the model map to database schema.EF then can translate queries that you write and LINQ to Enities against your classes into SQL that is understood by your database  , and then executes tour queery and uses the result t return populated instances of your objects.

 

EF Core Enables Long Requested Features that were not in EF 6:

  • Ability to do batch inserts, updates ,and deletes , that’s something that’s never been supported in EF.
  • Able to specify unique foreign keys In entities .
  • LINQ Queries have become smarter .
  • Easy to work with entities and graphs in disconnected applications as well as to provide much more consistent behavior.
  • In-Memory provider for testing that don’t need to hit the database.
  • Mapping to backing fields and IEnumerable.

You can get more related information on gitHub through this link https://github.com/aspnet/EntityFramework/wiki/Roadmap.

Happy Coding !!

Leave a Comment

Your email address will not be published.