问题描述
我有DbContext连接到我的数据库。在启动时使用依赖项注入配置了上下文。
join
我还有一项服务,可用于从数据库中获取数据并将其像索引一样保存到搜索引擎(Lucene),启动程序后仅需要一次,而我正尝试在Startup中调用该方法.cs。问题在于该服务使用了DbContext,我无法为其声明实例。
SELECT album_singer,album_name,album_format_name,album_tracks
FROM album
JOIN album_format ON album.Album_Type_ID = album_format.Album_Format_ID
ORDER BY FIELD(album_format_name,'Vinyl','Digital','Cassette','Compact disc'),album_tracks ASC
我正在尝试在启动时调用AddDataToSearchDocuments()方法,我只需要执行一次,但是由于需要上下文,它无法正常工作。您有什么想法吗?
我尝试过
public static class MyPhotoServiceCollectionExtension
{
public static IServiceCollection AddMyPhoto(this IServiceCollection services,IConfiguration configuration)
{
services.AddDbContext<MyPhotoDbContext>(options =>
{
options.UsesqlServer(configuration.GetConnectionString("DefaultConnection"));
});
return services;
}
}
public class SearchDataService
{
private readonly MyPhotoDbContext _context;
public SearchDataService(MyPhotoDbContext context)
{
_context = context;
}
public void AddDataToSearchDocuments()
{
var allPhotos = _context.Images.Include(x => x.Owner).Where(x => x.ImageType == "photo");
var allVideos = _context.Images.Where(x => x.ContentType.Contains("video")).Include(x => x.Owner);
var users = _context.Users.Include(x => x.Avatar);
.....///adding to search documents
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)