InMemory
Improve EF Core performance with EF Extensions
InMemory
InMemory Provider
InMemory is designed to be a general-purpose database for testing and is not designed to mimic a relational database.
InMemory will allow you to save data that would violate referential integrity constraints in a relational database.
If you use DefaultValueSql(string) for a property in your model, this is a relational database API and will not affect when running against InMemory.
Concurrency via Timestamp/row version ([Timestamp] or IsRowVersion) is not supported.
No
DbUpdateConcurrencyException
will be thrown if an update is done using an old concurrency token.
Install Entity Framework Core
Let's create a new application using the Console App (.NET Core) template and install Microsoft.EntityFrameworkCore. It is available as a nuget package and you can install it using Nuget Package Manager.
In the Package Manager Console window, enter the following command.
You can also install it by right-clicking on your project in Solution Explorer and select Manage Nuget Packages...
Search for Microsoft.EntityFrameworkCore and install the latest version by pressing the install button. It doesn't have additional logic that won't apply to all scenarios.
For example, EF Core will need to know what database or datastore you plan on working with and who those providers are in individual packages.
Register EF Core Provider
For InMemory, we need to install Microsoft.EntityFrameworkCore.InMemory and will get all the packages required for EF Core.
Now, you are ready to start your application.
Create Data Model
Model is a collection of classes to interact with the database.
A model stores data that is retrieved according to the Controller's commands and displayed in the View.
It can also be used to manipulate the data to implement the business logic.
To create a data model for our application, we will start with the following two entities.
There's a one-to-many relationship between Author
and Book
entities. In other words, an author can write any number of books, and a book can be written by only one author.
Create Database Context
The database context class provides the main functionality to coordinate Entity Framework with a given data model.
You create this class by deriving from the
System.Data.Entity.DbContext
class.In your code, you specify which entities are included in the data model.
You can also customize certain Entity Framework behaviors.
So, let's add a new BookStore
class which will inherit the DbContext
class.
In EF Core, the DbContext
has a virtual method called OnConfiguring
which will get called internally by EF Core.
It will pass in an
optionsBuilder
instance which can be used to configure options for theDbContext
.The
optionsBuilder
hasUseInMemoryDatabase
method which expects a connection string as a parameter.
Now, we are done with the required classes and database creation, let's add some authors and book records to the database and then retrieve them.
References
If you run the application, you will see that authors and books are successfully inserted into the database.
Last updated