I have implemented the EntityFrameworkFileProvider
for my ASP.NET core web application, I want the ViewDbContext
instance to be injected by ASP.NET core DI framework in the constructor:
(ViewDbContext
is a dbContext
)
public class EntityFrameworkFileProvider : IFileProvider
{
private ViewDbContext _context;
public EntityFrameworkFileProvider(ViewDbContext context)
{
/* should be injected by asp.net core DI */
_context = context;
}
public IDirectoryContents GetDirectoryContents(string subpath)
{
.....
}
public IFileInfo GetFileInfo(string subpath)
{
var result = new DatabaseFileInfo(_context, subpath);
return result.Exists ? result as IFileInfo : new NotFoundFileInfo(subpath);
}
public IChangeToken Watch(string filter)
{
return new DatabaseChangeToken(_context, filter);
}
}
Now I add the EntityFrameworkFileProvider
to RazorViewEngineOption
in startup.cs
How to make the ViewDbContext
instance to be automatically injected by DI framework in the ConfigureServices
method of startup.cs? how should i call the EntityFrameworkFileProvider
constructor correctly?
In Startup.cs
public void ConfigureServices(IServiceCollection services)
{
/* Add EntityFrameworkFileProvider to Razor engine */
services.Configure<RazorViewEngineOptions>(opts =>
{
opts.FileProviders.Add(new EntityFrameworkFileProvider(null?));
});
services.AddMvc();
}