Tomas Herceg
Microsoft MVP
Microsoft Regional Directory
@hercegtomas https://www.tomasherceg.com/blog
Entity Framework Core 2.1 – what’s new?
Difference between EF 6 and EF Core
▪ Entity Framework 6
▪ Old codebase
▪ Difficult to extend
▪ Lot of legacy API included for compatibility
▪ ObjectContext
▪ DbContext
▪ High memory footprint
Difference between EF 6 and EF Core
▪ Entity Framework Core
▪ New implementation
▪ Lightweight
▪ Extensible
▪ Cross-platform (.NET Standard)
▪ Support of non-relational data stores
▪ Current state
▪ Some features of EF 6 missing
▪ Lot of new features not present in EF 6
▪ 2.0 stable, 2.1 preview 2
New Features in EF Core 2.1
▪ Data Seeding
▪ Lazy loading
▪ Parameters in entity constructors
▪ Value conversions
▪ GROUP BY translation
▪ and more…
▪ https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.1
Data Seeding
▪ Never really solved in EF 6
▪ EF Migrations with SQL(@”INSERT INTO …”)
▪ table.AddOrUpdate in Migrations configuration
▪ Someone just gave up
▪ No solution in EF Core until 2.1
▪ Data are part of model definition
▪ Migrations are aware of data
DEMO
Data Seeding
Tomas Herceg
Microsoft MVP
Microsoft Regional Directory
@hercegtomas https://www.tomasherceg.com/blog
Lazy loading
▪ Loved & hated in EF 6
▪ Eager loading
▪ context.Blogs
.Include(blog =>blog.Posts)
.ThenInclude(post => post.Comments)
▪ Explicit loading
▪ context.Entry(blog)
.Collection(blog => blog.Posts)
.Load()
▪ Projection
▪ query.Select(blog => new { … })
Lazy loading
▪ Must be turned on explicitly
▪ With proxies (easiest one)
▪ Separate NuGet package
▪ Microsoft.EntityFrameworkCore.Proxies
▪ Call UseLazyLoadingProxies()
▪ Without proxies
▪ Inject ILazyLoader or Action<object, string> in constructor
DEMO
Lazy loading
Tomas Herceg
Microsoft MVP
Microsoft Regional Directory
@hercegtomas https://www.tomasherceg.com/blog
Entities with constructor parameters
▪ Initialize entity properties with constructor
▪ Bound by naming convention
▪ firstName parameter  FirstName property
▪ Supports injecting of other services
▪ DbContext (ohh, ugly)
▪ ILazyLoader / Action<object, string>
▪ IEntityType (EF Core metadata about the entity type)
DEMO
Entity constructor parameters
Tomas Herceg
Microsoft MVP
Microsoft Regional Directory
@hercegtomas https://www.tomasherceg.com/blog
Value conversions
▪ Database types are mapped to .NET types
▪ Conversions were hard-coded in EF 6
▪ EF Core adds extensibility
▪ Many built-in converters
▪ BoolToZeroOneConverter and more
▪ BytesToStringConverter (base64)
▪ EnumToStringConverter
▪ TimeSpanToTicksConverter
▪ …
DEMO
Value conversions
Tomas Herceg
Microsoft MVP
Microsoft Regional Directory
@hercegtomas https://www.tomasherceg.com/blog
GROUP BY translation
▪ GROUP BY was evaluated in-memory until EF Core 2.1
▪ Huge issue
▪ Some limitations still remain
▪ Always check generated SQL
DEMO
GROUP BY translation
Tomas Herceg
Microsoft MVP
Microsoft Regional Directory
@hercegtomas https://www.tomasherceg.com/blog
Is it ready yet?
▪ Well, it depends...
▪ Advantages
▪ Offers more mapping features
▪ Supports .NET Core
▪ Faster for simple cases
▪ Easy fall back to raw SQL
▪ Disadvantages
▪ No support for complex queries
Future plans
▪ Product planning for 2.2 not ready yet
▪ Follow the GitHub repo
▪ https://github.com/aspnet/EntityFrameworkCore
▪ Cosmos DB provider
▪ Planned for 2.2
▪ Document DB API
▪ First large non-relational provider
Tomas Herceg
Microsoft MVP
Microsoft Regional Directory
@hercegtomas https://www.tomasherceg.com/blog
Q&A

Tomáš Herceg "Entity Framework Core 2.1 – what’s new?"

  • 1.
    Tomas Herceg Microsoft MVP MicrosoftRegional Directory @hercegtomas https://www.tomasherceg.com/blog Entity Framework Core 2.1 – what’s new?
  • 2.
    Difference between EF6 and EF Core ▪ Entity Framework 6 ▪ Old codebase ▪ Difficult to extend ▪ Lot of legacy API included for compatibility ▪ ObjectContext ▪ DbContext ▪ High memory footprint
  • 3.
    Difference between EF6 and EF Core ▪ Entity Framework Core ▪ New implementation ▪ Lightweight ▪ Extensible ▪ Cross-platform (.NET Standard) ▪ Support of non-relational data stores ▪ Current state ▪ Some features of EF 6 missing ▪ Lot of new features not present in EF 6 ▪ 2.0 stable, 2.1 preview 2
  • 4.
    New Features inEF Core 2.1 ▪ Data Seeding ▪ Lazy loading ▪ Parameters in entity constructors ▪ Value conversions ▪ GROUP BY translation ▪ and more… ▪ https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.1
  • 5.
    Data Seeding ▪ Neverreally solved in EF 6 ▪ EF Migrations with SQL(@”INSERT INTO …”) ▪ table.AddOrUpdate in Migrations configuration ▪ Someone just gave up ▪ No solution in EF Core until 2.1 ▪ Data are part of model definition ▪ Migrations are aware of data
  • 6.
    DEMO Data Seeding Tomas Herceg MicrosoftMVP Microsoft Regional Directory @hercegtomas https://www.tomasherceg.com/blog
  • 7.
    Lazy loading ▪ Loved& hated in EF 6 ▪ Eager loading ▪ context.Blogs .Include(blog =>blog.Posts) .ThenInclude(post => post.Comments) ▪ Explicit loading ▪ context.Entry(blog) .Collection(blog => blog.Posts) .Load() ▪ Projection ▪ query.Select(blog => new { … })
  • 8.
    Lazy loading ▪ Mustbe turned on explicitly ▪ With proxies (easiest one) ▪ Separate NuGet package ▪ Microsoft.EntityFrameworkCore.Proxies ▪ Call UseLazyLoadingProxies() ▪ Without proxies ▪ Inject ILazyLoader or Action<object, string> in constructor
  • 9.
    DEMO Lazy loading Tomas Herceg MicrosoftMVP Microsoft Regional Directory @hercegtomas https://www.tomasherceg.com/blog
  • 10.
    Entities with constructorparameters ▪ Initialize entity properties with constructor ▪ Bound by naming convention ▪ firstName parameter  FirstName property ▪ Supports injecting of other services ▪ DbContext (ohh, ugly) ▪ ILazyLoader / Action<object, string> ▪ IEntityType (EF Core metadata about the entity type)
  • 11.
    DEMO Entity constructor parameters TomasHerceg Microsoft MVP Microsoft Regional Directory @hercegtomas https://www.tomasherceg.com/blog
  • 12.
    Value conversions ▪ Databasetypes are mapped to .NET types ▪ Conversions were hard-coded in EF 6 ▪ EF Core adds extensibility ▪ Many built-in converters ▪ BoolToZeroOneConverter and more ▪ BytesToStringConverter (base64) ▪ EnumToStringConverter ▪ TimeSpanToTicksConverter ▪ …
  • 13.
    DEMO Value conversions Tomas Herceg MicrosoftMVP Microsoft Regional Directory @hercegtomas https://www.tomasherceg.com/blog
  • 14.
    GROUP BY translation ▪GROUP BY was evaluated in-memory until EF Core 2.1 ▪ Huge issue ▪ Some limitations still remain ▪ Always check generated SQL
  • 15.
    DEMO GROUP BY translation TomasHerceg Microsoft MVP Microsoft Regional Directory @hercegtomas https://www.tomasherceg.com/blog
  • 16.
    Is it readyyet? ▪ Well, it depends... ▪ Advantages ▪ Offers more mapping features ▪ Supports .NET Core ▪ Faster for simple cases ▪ Easy fall back to raw SQL ▪ Disadvantages ▪ No support for complex queries
  • 17.
    Future plans ▪ Productplanning for 2.2 not ready yet ▪ Follow the GitHub repo ▪ https://github.com/aspnet/EntityFrameworkCore ▪ Cosmos DB provider ▪ Planned for 2.2 ▪ Document DB API ▪ First large non-relational provider
  • 18.
    Tomas Herceg Microsoft MVP MicrosoftRegional Directory @hercegtomas https://www.tomasherceg.com/blog Q&A