Hi @mc
can I create a real database in disk and create in-memory database? when read I use in-memory database when write I write to both in-memory and real database. but how to load all data in startup?
EF Core In-Memory Database Provider allows Entity Framework Core to be used with an in-memory database, the use is similar to use the EF core with SQL Server database.
First, you need to install the Microsoft.EntityFrameworkCore.InMemory package, then add the DBContext:
public class InMemoryDbContext:DbContext
{
public InMemoryDbContext(DbContextOptions<InMemoryDbContext> options) : base(options)
{
}
public DbSet<InAuthor> Authors { get; set; }
public DbSet<InBook> Books { get; set; }
}
Then, add the class and repository service to add the initial data:
public interface IAuthorRepository
{
public List<InAuthor> GetAuthors();
public void InitAuthorInMemoryDb();
}
public class AuthorRepository : IAuthorRepository
{
private readonly InMemoryDbContext _dbContext;
public AuthorRepository(InMemoryDbContext inMemoryDbContext)
{
_dbContext=inMemoryDbContext;
}
public void InitAuthorInMemoryDb()
{
var authors = new List<InAuthor>
{
new InAuthor
{
FirstName ="Joydip",
LastName ="Kanjilal",
Books = new List<InBook>()
{
new InBook { Title = "Mastering C# 8.0"},
new InBook { Title = "Entity Framework Tutorial"},
new InBook { Title = "ASP.NET 4.0 Programming"}
}
},
new InAuthor
{
FirstName ="Yashavanth",
LastName ="Kanetkar",
Books = new List<InBook>()
{
new InBook { Title = "Let us C"},
new InBook { Title = "Let us C++"},
new InBook { Title = "Let us C#"}
}
}
};
_dbContext.Authors.AddRange(authors);
_dbContext.SaveChanges();
}
public List<InAuthor> GetAuthors()
{
var list = _dbContext.Authors
.Include(a => a.Books)
.ToList();
return list;
}
}
register the DbContext and Service:
builder.Services.AddDbContext<InMemoryDbContext>(options => options.UseInMemoryDatabase("ImMemoryDb"));
builder.Services.AddScoped<InMemoryDbContext>();
builder.Services.AddScoped<IAuthorRepository, AuthorRepository>();
After that, to resolve the service and call the dbcontext at app start up, you can refer to the following code:
builder.Services.AddDbContext<InMemoryDbContext>(options => options.UseInMemoryDatabase("ImMemoryDb"));
builder.Services.AddScoped<InMemoryDbContext>();
builder.Services.AddScoped<IAuthorRepository, AuthorRepository>();
var _AuthorRepository = builder.Services.BuildServiceProvider().GetService<IAuthorRepository>();
_AuthorRepository?.InitAuthorInMemoryDb();
var inmemorudbcontext = builder.Services.BuildServiceProvider().GetService<InMemoryDbContext>();
var authors = inmemorudbcontext?.Authors.ToList();
...
using (var serviceScope = app.Services.CreateScope()) {
var services = serviceScope.ServiceProvider;
var _authorrepo = services.GetService<IAuthorRepository>();
var result1 = _authorrepo?.GetAuthors().ToList();
var _dbcontext = services.GetService<InMemoryDbContext>();
var result2 = _dbcontext?.Authors.ToList();
}
The output as below:
Note: 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. For production use, you could consider using Redis.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion