February 13, 2018 What’s New in EF Core? How do you manage diverse data sets across multiple databases and maintain code quality? Use EF Core, which streamlines technical strategy and expenses. Nikola Genov Entity Framework Core (EF Core) is a cross-platform, lightweight, and extensible ORM framework. EF Core is the latest version of Entity Framework. A complete rewrite of the previous version, it was released along with .NET Core. To get started, simply install the packages for the providers you will use. The EF Core source code is also available on GitHub. How Businesses Cut the Fat with EF Core As cloud computing and SaaS only grow in their pervasiveness, the architecture of the new version of EF Core adapts to modern business and technology needs in its extensibility. By offering EF Core in a more lightweight and scalable model, businesses looking to improve their technological capabilities with an eye to streamlined expenses may do so with the new architecture of EF Core. What Are the New Features in EF Core? EF Core is Modular and Lightweight EF Core has been decomposed into smaller packages and you can use only those that are useful to your project. You don’t need to install the full set of features. If you want to use only the SQL Server provider, you can install it using the NuGet Package Manager: Install-Package Microsoft.EntityFrameworkCore.SqlServer EF Core Boasts Cross-Platform Functionality Microsoft continues to decouple its services, frameworks, and applications from the Windows operating system. For example, SQL Server 2017 works on Windows, Linux, and Docker containers. The same applies to the new .NET Core framework, which can also run on Mac OS. EF Core can connect to a variety of relational or NoSQL databases using different database providers. EF Core Eases Testing with an In-Memory Database Provider EF Core also incorporates the ability to use an in-memory database provider. Using an in-memory database allows teams to work with data in a more lightweight fashion. When the business does not need to rely on a physical database to test large amounts of code they can save themselves the costs associated running a physical database on premises. The In-Memory provider is useful when you want to test components using a tool that approximates connecting to the real database, without the overhead of actual database operations. It is designed to be a general purpose database for testing, and is not designed to mimic a relational database. If you want to test against something that behaves more like a true relational database, then consider using SQLite in-memory mode. In-Memory provider is going to benefit mostly automated testing, which supports the practice of continuous integration. This makes it easier for teams to deploy and test their software more frequently while reducing downtime. This ensures that teams are always able to improve software quality as user needs change over time. Shadow Properties in EF Core Shadow properties allow developers to add properties without affecting the domain model at the center of their work. This helps teams to maintain a simpler domain model that exposes less data in undesired ways. The value and state of these properties is maintained purely in the Change Tracker. They can also participate in LINQ query, database migration, and CRUD operations. Each time that a new Shadow Property is added to a data model, a migration is added and run against the target database. Thus, it is easy to keep track of the shadow properties and how they are applied. Shadow properties can be configured using the Fluent API in the OnModelCreating method. Use EF Core to Improve Shadow Property Tracking Here is an example that illustrates a LastUpdated shadow property added to the Contact entity: public class Contact { public int ContactId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } } public class AppDbContext : IdentityDbContext<ApplicationUser> { public DbSet<Contact> Contacts { get; set; } protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<Contact>() .Property<DateTime>("LastUpdated"); } } Then we can access the shadow property via the DbContext.Entry property and set its value via the CurrentValue property: var context = new AppDbContext(); var contact = new Contact { FirstName = "John", LastName = "Lennon" }; context.Add(contact); context.Entry(contact).Property("LastUpdated") .CurrentValue = DateTime.UtcNow; context.SaveChanges(); And finally shadow properties can be used in LINQ queries via the static Property method of the EF utility class: var contacts = context.Contacts.OrderBy(contact => EF.Property<DateTime>(contact, "LastUpdated")); EF Core is New. But is it Improved? Teams familiar with the older versions of EF, like EF6, should keep in mind that EF Core is missing some of the features that they may be used to. The following features have yet to be implemented in EF Core: Fully-Implemented Stored Procedures — These allowed for more efficient server-side data processing and operations such as inserts, updates, and deletions. Support for Spatial Types — The ability to query and use spatial data types is not available in this version of EF Core. Support for Lazy Loading — EF Core allows you to use the navigation properties in your model to load related entities. But only eager (data is loaded from the database as part of the initial query) and explicit (data is explicitly loaded from the database at a later time) loading are currently supported in this version of EF Core. GroupBy Support in a LINQ Query — Currently it falls back silently to LINQ to Objects, which means it brings everything from the database and groups in-memory. This is a highly inefficient way to process large amounts of data. For now it is advisable to use plain SQL instead of LINQ grouping whenever possible. Many-to-Many Collections — Many-to-many relationships in EF Core are without a middle entity/table. In order to create such a relationship, map the junction table to an entity. Table Per Type & Table Per Concrete Class Inheritance Strategies – Currently, only the table-per-hierarchy (TPH) pattern is implemented in EF Core. Map Database Views — The ability to map views instead of tables is missing in EF Core Populate Non-Model Types from SQL — This turns the results of custom SQL into classes that are not part of the model Image Source: Unsplash, Alexandru Gogan Tags Development Share Share on Facebook Share on LinkedIn Share on Twitter Cloud Computing How does cloud development control expenses and reliably scale application delivery? Download Share Share on Facebook Share on LinkedIn Share on Twitter Sign up for our monthly newsletter. Sign up for our monthly newsletter.