Why not loading data in background thread and then store in Cache, then it will be accessible through out the application, Program.cs is simple generic host implementation and you can do operation like we do in simple console class.
Usually you cache data depending on their usage (for example frequently used mostly read only data) and the app doesn't really care directly about that. Some db have also features that could help such as in memory tables..
Coming up wiht the right architecture is a little more complex that just "loading the whole db in memory" (or you are working on read only data ?)
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env,IMemoryCache cache)
{
//other middleware...
var scopeFactory = app.ApplicationServices.GetService<IServiceScopeFactory>();
using (var scope = scopeFactory.CreateScope())
{
var provider = scope.ServiceProvider;
using (var dbContext = provider.GetRequiredService<YourDbContext>())
{
tests= dbContext.Tests.ToList();
}
}
cache.Set("tests", tests);
app.UseMvc();
}
2.Controller:
private readonly IMemoryCache _cache;
public HomeController(IMemoryCache cache)
{
_cache = cache;
}
public IActionResult Index()
{
var data = _cache.Get("tests");
return View(data);
}
Best Regards,
Rena
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
223 Points
128 Posts
Load the table data from DB on startup of application and share across the application
Nov 12, 2019 09:31 AM|ShivPankaj|LINK
Hi all,
I want to load the DB table data on startup of the application and want to use it throughout the application.
Can anyone help me to do the same, Thanks in advance!
Pankaj Kumar
Contributor
2096 Points
1040 Posts
Re: Load the table data from DB on startup of application and share across the application
Nov 12, 2019 10:35 AM|Khuram.Shahzad|LINK
Why not loading data in background thread and then store in Cache, then it will be accessible through out the application, Program.cs is simple generic host implementation and you can do operation like we do in simple console class.
All-Star
48490 Points
18071 Posts
Re: Load the table data from DB on startup of application and share across the application
Nov 12, 2019 10:54 AM|PatriceSc|LINK
Hi,
Usually you cache data depending on their usage (for example frequently used mostly read only data) and the app doesn't really care directly about that. Some db have also features that could help such as in memory tables..
Coming up wiht the right architecture is a little more complex that just "loading the whole db in memory" (or you are working on read only data ?)
Contributor
2690 Points
874 Posts
Re: Load the table data from DB on startup of application and share across the application
Nov 13, 2019 07:07 AM|Rena Ni|LINK
Hi ShivPankaj,
Here is a simple demo like below:
1.Startup.cs:
2.Controller:
Best Regards,
Rena