concept EF in category .net

This is an excerpt from Manning's book .NET Core in Action.
Alongside ASP.NET Core, Microsoft is building Entity Framework (EF) Core. EF Core is a .NET Core version of Entity Framework, which is a full ORM built by Microsoft for the .NET Framework. Instead of porting EF from the .NET Framework, the team decided to rewrite most of EF—much like how ASP.NET Core is a rewrite of ASP.NET. You’ll explore EF briefly by using it to rewrite your supply-chain management data-access layer.
Listing 6.24. EfScmContext—ScmContext modified for use with EF
using System.Collections.Generic; using Microsoft.EntityFrameworkCore; namespace EfScmDataAccess { public class EfScmContext : DbContext #1 { public DbSet<PartType> Parts { get; set; } #2 public DbSet<InventoryItem> Inventory { get; set; } public DbSet<Supplier> Suppliers { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite("Filename=efscm.db"); #3 } } }That’s basically all you need to do for the data-access code. EF generates the SQL commands necessary to perform the create, retrieve, update, and delete operations that you want. These operations are all available on the DbSet class, which we’ll look into later.
Now you have to create the database schema, and EF can generate it for you. It doesn’t do this when your application starts up, though. Instead, you need to use EF’s .NET CLI migration tool to apply the schema to the database. This also means that if you want EF to create the database schema, you can’t use an in-memory Sqlite database. .NET CLI tools are pulled in as dependencies.
In our example, the EfScmContext object has the connection to the SQLite database written in its constructor, but EF has plenty of other ways to construct the context and connect it to a database. When using EF, you don’t have to build a custom data-access layer with DI. The configuration extension library may be helpful, though.
DbSet operates much like a collection. When getting values from DbSet, your LINQ queries will be interpreted into SQL statements by EF. So just because you have thousands of parts in your inventory, doesn’t mean thousands of PartType objects will be held in memory. EF keeps track of the objects you retrieve in the context. If you make a change to an object and save it to DbSet, EF detects the change you made and generates the SQL UPDATE statement.
EF handles a lot of the work of communicating with a database—it has many providers to work with all kinds of databases. But regardless of the database you connect to, the code remains the same. Developers write queries using LINQ instead of SQL. For many applications, this can significantly boost productivity over writing a custom data-access layer.