Introduction to Entity Framework Core April 1, 2017Posted by fofo in Entity framework.
Tags: Entity framework, Entity Framework Core, ORM
add a comment
In this post I am going to look into Entity Framework Core, present the new features, the similarities and differences from EF6.
Ι hope that the people who will read this post are familiar with what EF is. In a nutshell EF is the official data access technology platform from Microsoft. It is a mature platform since it is almost 9 years old. It is a new way of accessing and storing data, a set of .NET APIs for accessing data from any data store.
Going into more details Entity Framework fits into a category of data access technologies called Object Relational Mappers or ORMs.
ORMs reduce the friction between how data is structured in a relational database and how you define your classes. Without an ORM, we typically have to write a lot of code to transform database results into instances of the classes. An ORM allows us to express our queries using our classes, and then the ORM builds and executes the relevant SQL for us, as well as materializing objects from the data that came back from the database. Βy using an ORM we can really eliminate redundant data interaction tasks and we can really enhance developer productivity. Instead of writing the relevant SQL to target whatever relational database you’re working with, Entity Framework uses the LINQ syntax that’s part of the .NET framework. LINQ to Entities allows developers to use strongly-typed query language regardless of which database they’re targeting.
When we use EF Core in your application, firstly you need to create your domain classes. These are pure .Net classes or objects and have nothing to do with EF Core.Then you use Entity Framework Core APIs to define a data model based on those domain classes. You also use Entity Framework APIs to write and execute LINQ to Entities Queries against those classes. When you need to save data back to the database you use SaveChanges methods. EF Core keeps track of the state of objects that it’s aware of, it’ll determine the SQL it needs to save data back to the database. Entity Framework will transform your LINQ to Entities Queries into SQL, execute that SQL, and then create objects from query results.
As you many know, Entity Framework development was moved to CodePlex and became open source. Entity Framework 6 has since then been moved to GitHub. The URL is github.com/aspnet/EntityFramework.
You can download and experiment with the latest version, you can clone the repository, add your own fixes/features, and then submit those as pull requests to become part of Entity Framework Core. All pull requests that are submitted from the community are examined from the ADO.Net team before becoming part of EF Core.
Another thing I wanted to mention is that different people call EF with different names. Let me be clear on that so that there is no confusion.
EF Core was released in late June of 2016 and it was called Entity Framework 7, but in January 2016, its name was changed to Entity Framework Core. At the same time ASP.NET 5 was changed to ASP.NET Core.
EF Core is not simply an update from EF6, it’s a different kind of Entity Framework, it is a complete rewrite.
For developers who have invested time and effort on EF6 and have projects on EF6 should not worry as it will be actively supported.
Entity Framework Core can run on .NET Core. NET Core can run on the CoreCLR and CoreCLR can run natively, not only in Windows, but also on Mac and Linux.
EF Core can also run inside the full .NET Framework that is any version that is 4.5.1 or newer.
Entity Framework Core is a brand new set of APIs and it doesn’t have all of the features that you might be used to with Entity Framework so it’s important to understand that before starting a new project with Entity Framework Core.
If you want to target cross platform or UWP apps then you have no other way but to use Entity Framework Core. For .NET apps that you want to run on Windows, you can still use Entity Framework 6. If you are building an ASP.NET Core applications that will run on Windows you can still use Entity Framework 6, so bear that in mind as well.
There are Entity Framework features that will never be part of the Entity Framework Core, for example in EF Core there is no support for a designer-based model, there’s no EDMX and it isn’t supported by the Entity Framework Designer.
Having said that in EF Core you can can still define a data model with classes and a DbContext. The DbContext API is still there and so is DbSets.
You can also create and migrate to the database as your model changes, and you can still query with LINQ to Entities. Entity Framework continues to track changes to entities in memory.
There are some new features in EF Core that there were no available in earlier versions. In EF Core we have the ability to do batch inserts, updates, and deletes.
We can specify unique foreign keys in entities and LINQ queries have become smarter and more efficient.
In EF Core there is an In Memory provider that makes it really easy to build automated tests using Entity Framework without hitting the database.
EF Core makes it really easy to use with inversion of control patterns and dependency injection. EF Core has the ability to populate backing fields not just properties. EF Core supports mapping to IEnumerables.
Hope you have a better understanding of EF Core now.
Hope it helps!!!