BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News What's New in Entity Framework Core 8

What's New in Entity Framework Core 8

This item in japanese

Entity Framework Core 8, scheduled for release in November 2023, brings new features and improvements to features already introduced in EF Core 7. Notable features include support for raw SQL queries for unmapped types, lazy-loading improvements, and support for TimeOnly and DateOnly SQL Server data types.

EF7 already introduced Raw SQL queries for scalar types, however, in EF8, raw SQL queries can now return any mappable CLR type not included in the Entity Framework model. Queries executed this way support Entity Framework features such as parametrized constructors and mapping attributes. Notably, relationships between the un-mapped type and other types in the model are not supported, since the un-mapped model cannot have foreign keys defined.

SQL views and functions can also be queried using this method, and can be mapped to any CLR type in the same way tables can be mapped.

Entity Framework 8 added support for lazy-loading of navigation properties in un-tracked queries. Lazy-loading properties on un-tracked queries work for lazy-loading with and without proxies.

There are some caveats to this approach though. Lazy-loading works only until the DbContext used to execute the no-tracking query is disposed of. Lazy loading will likewise not work for entities detached from the DbContext. Additionally, lazy-loading will always use synchronous I/O since there is no way to asynchronously access a navigation property.

The lazy-load feature would silently not run when the navigation property did not have the virtual keyword in EF6 applications, causing bugs.

In EF Core, to avoid the common bug, the proxies throw an exception when the virtual keyword is not present on the navigation property. Due to this new EF Core behavior, EF Core 8 introduces an opt-out feature that you can use to eagerly load only specific navigation properties you explicitly set to load in such a way.

The DateOnly (mapping to a date type column) and TimeOnly (mapping to a time type column) data types are now natively supported by EF Core 8 for SQL Server. Until now (In EF Core 6 and 7), you could use these types only by installing a community NuGet package. The two new types can also be used in SQL JSON columns.

A medium-impact breaking change is related to these types as well. In previous versions of EF, the date and time column types were scaffolded as DateTime and TimeSpan types respectively. In EF8, the new DateOnly and TimeOnly type mappings are used instead. This only affects users that use a database-first approach and regularly re-scaffold their code model. A mitigation strategy is available for users affected by this change.

You can read more about plans for EF8 from official Microsoft sources and vote on features you would like to see implemented on the EF Core GitHub issues page.

You can download the latest preview of EF Core 8, released on 21st February 2023, as a NuGet package. It supports .NET 6 and .NET 7 projects.

About the Author

Rate this Article

Adoption
Style

BT