Skip to content

Update guidance around in-memory database #4254

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions entity-framework/core/dbcontext-configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,15 @@ These `Use*` methods are extension methods implemented by the database provider.

The following table contains examples for common database providers.

| Database system | Example configuration | NuGet package
|:-----------------------------|-----------------------------------------------|--------------
| SQL Server or Azure SQL | `.UseSqlServer(connectionString)` | [Microsoft.EntityFrameworkCore.SqlServer](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer/)
| Azure Cosmos DB | `.UseCosmos(connectionString, databaseName)` | [Microsoft.EntityFrameworkCore.Cosmos](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Cosmos/)
| SQLite | `.UseSqlite(connectionString)` | [Microsoft.EntityFrameworkCore.Sqlite](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Sqlite/)
| EF Core in-memory database | `.UseInMemoryDatabase(databaseName)` | [Microsoft.EntityFrameworkCore.InMemory](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.InMemory/)
| PostgreSQL* | `.UseNpgsql(connectionString)` | [Npgsql.EntityFrameworkCore.PostgreSQL](https://www.nuget.org/packages/Npgsql.EntityFrameworkCore.PostgreSQL/)
| MySQL/MariaDB* | `.UseMySql(connectionString)` | [Pomelo.EntityFrameworkCore.MySql](https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql/)
| Oracle* | `.UseOracle(connectionString)` | [Oracle.EntityFrameworkCore](https://www.nuget.org/packages/Oracle.EntityFrameworkCore/)
| Database system | Example configuration | NuGet package |
|:---------------------------|----------------------------------------------|--------------------------------------------------------------------------------------------------------------------|
| SQL Server or Azure SQL | `.UseSqlServer(connectionString)` | [Microsoft.EntityFrameworkCore.SqlServer](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer/) |
| Azure Cosmos DB | `.UseCosmos(connectionString, databaseName)` | [Microsoft.EntityFrameworkCore.Cosmos](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Cosmos/) |
| SQLite | `.UseSqlite(connectionString)` | [Microsoft.EntityFrameworkCore.Sqlite](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Sqlite/) |
| EF Core in-memory database | `.UseInMemoryDatabase(databaseName)` | [Microsoft.EntityFrameworkCore.InMemory](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.InMemory/) |
| PostgreSQL* | `.UseNpgsql(connectionString)` | [Npgsql.EntityFrameworkCore.PostgreSQL](https://www.nuget.org/packages/Npgsql.EntityFrameworkCore.PostgreSQL/) |
| MySQL/MariaDB* | `.UseMySql(connectionString)` | [Pomelo.EntityFrameworkCore.MySql](https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql/) |
| Oracle* | `.UseOracle(connectionString)` | [Oracle.EntityFrameworkCore](https://www.nuget.org/packages/Oracle.EntityFrameworkCore/) |

*These database providers are not shipped by Microsoft. See [Database Providers](xref:core/providers/index) for more information about database providers.

Expand Down
17 changes: 10 additions & 7 deletions entity-framework/core/providers/in-memory/index.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
---
title: InMemory Database Provider - EF Core
description: Information on the Entity Framework Core InMemory database provider
title: In-memory Database Provider - EF Core
description: Information on the Entity Framework Core in-memory database provider
author: ajcvickers
ms.date: 10/27/2016
ms.date: 02/11/2023
uid: core/providers/in-memory/index
---
# EF Core In-Memory Database Provider

This database provider allows Entity Framework Core to be used with an in-memory database. While some users use the in-memory database for testing, this is generally discouraged; the SQLite provider in in-memory mode is a more appropriate test replacement for relational databases. For more information on how to test EF Core applications, see the [testing documentation](xref:core/testing/index). The provider is maintained as part of the [Entity Framework Core Project](https://github.com/dotnet/efcore).
This database provider allows Entity Framework Core to be used with an in-memory database. While some users use the in-memory database for testing, this is discouraged. For more information on how to test EF Core applications, see the [_Testing EF Core Applications_](xref:core/testing/index). The provider is maintained by Microsoft as part of the [Entity Framework Core Project](https://github.com/dotnet/efcore).

> [!WARNING]
> The In-Memory provider was not designed for use outside of testing environments and should never be used as such.
> The EF Core in-memory database is not designed for performance or robustness and should not be used outside of testing environments. It is not designed for production use.

> [!IMPORTANT]
> New features are not being added to the in-memory database.

## Install

Expand All @@ -34,9 +37,9 @@ Install-Package Microsoft.EntityFrameworkCore.InMemory

The following resources will help you get started with this provider.

* [Testing with InMemory](xref:core/testing/testing-without-the-database#inmemory-provider)
* [Testing with in-memory (not recommended)](xref:core/testing/testing-without-the-database#inmemory-provider)
* [UnicornStore Sample Application Tests](https://github.com/rowanmiller/UnicornStore/blob/master/UnicornStore/src/UnicornStore.Tests/Controllers/ShippingControllerTests.cs)

## Supported Database Engines

In-process memory database, designed for testing purposes only.
In-process naive, non-performant, and non-persisted in-memory database. Not designed for production use.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The following API and behavior changes have the potential to break existing appl
| [Value generators are called when the entity state is changed from Detached to Unchanged, Updated, or Deleted](#non-added-generation) | Low |
| [IMigrationsModelDiffer now uses IRelationalModel](#relational-model) | Low |
| [Discriminators are read-only](#read-only-discriminators) | Low |
| [Provider-specific EF.Functions methods throw for InMemory provider](#no-client-methods) | Low |
| [Provider-specific EF.Functions methods throw for in-memory provider](#no-client-methods) | Low |
| [IndexBuilder.HasName is now obsolete](#index-obsolete) | Low |
| [A pluralizer is now included for scaffolding reverse engineered models](#pluralizer) | Low |
| [INavigationBase replaces INavigation in some APIs to support skip navigations](#inavigationbase) | Low |
Expand Down Expand Up @@ -525,21 +525,21 @@ modelBuilder.Entity<BaseEntity>()

<a name="no-client-methods"></a>

### Provider-specific EF.Functions methods throw for InMemory provider
### Provider-specific EF.Functions methods throw for in-memory provider

[Tracking Issue #20294](https://github.com/dotnet/efcore/issues/20294)

#### Old behavior

Provider-specific EF.Functions methods contained implementation for client execution, which allowed them to be executed on the InMemory provider. For example, `EF.Functions.DateDiffDay` is a Sql Server specific method, which worked on InMemory provider.
Provider-specific EF.Functions methods contained implementation for client execution, which allowed them to be executed on the in-memory provider. For example, `EF.Functions.DateDiffDay` is a Sql Server specific method, which worked on InMemory provider.

#### New behavior

Provider-specific methods have been updated to throw an exception in their method body to block evaluating them on client side.

#### Why

Provider-specific methods map to a database function. The computation done by the mapped database function can't always be replicated on the client side in LINQ. It may cause the result from the server to differ when executing the same method on client. Since these methods are used in LINQ to translate to specific database functions, they don't need to be evaluated on client side. As InMemory provider is a different _database_, these methods aren't available for this provider. Trying to execute them for InMemory provider, or any other provider that doesn't translate these methods, throws an exception.
Provider-specific methods map to a database function. The computation done by the mapped database function can't always be replicated on the client side in LINQ. It may cause the result from the server to differ when executing the same method on client. Since these methods are used in LINQ to translate to specific database functions, they don't need to be evaluated on client side. As the in-memory provider is a different _database_, these methods aren't available for this provider. Trying to execute them for InMemory provider, or any other provider that doesn't translate these methods, throws an exception.

#### Mitigations

Expand Down
2 changes: 1 addition & 1 deletion entity-framework/core/what-is-new/ef-core-5.0/whatsnew.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,4 @@ For further information, [see the full documentation on event counters](xref:cor
* The EF Core command line tools now automatically configure the `ASPNETCORE_ENVIRONMENT` *and* `DOTNET_ENVIRONMENT` environment variables to "Development". This brings the experience when using the generic host in line with the experience for ASP.NET Core during development.
* Custom command-line arguments can be flowed into <xref:Microsoft.EntityFrameworkCore.Design.IDesignTimeDbContextFactory%601>, allowing applications to control how the context is created and initialized.
* The index fill factor can now be [configured on SQL Server](xref:core/providers/sql-server/indexes#fill-factor).
* The new <xref:Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.IsRelational%2A> property can be used to distinguish when using a relational provider and a non-relation provider (such as InMemory).
* The new <xref:Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.IsRelational%2A> property can be used to distinguish when using a relational provider and a non-relation provider (such as in-memory).
2 changes: 1 addition & 1 deletion entity-framework/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@
href: core/providers/cosmos/functions.md
- name: End-to-end sample
href: core/providers/cosmos/planetary-docs-sample.md
- name: InMemory (for testing)
- name: In-memory (not recommended)
href: core/providers/in-memory/index.md
- name: Write a database provider
href: core/providers/writing-a-provider.md
Expand Down