how to load all data in to memory?

mc 5,426 Reputation points
2023-07-19T07:55:15.86+00:00

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?

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-07-20T06:03:42.68+00:00

    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:

    User's image

    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

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-07-19T15:37:47.1033333+00:00

    what are you using for a in-memory database? if full featured, often they are slower than the disk based database. also unless you use distributed memory, this design limits you to a single server.

    any how you load the data depends on the databases. at startup you would read all rows of one and write to the other. if it is a memory cache like redis, then load on cache misses.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.