[英]Missing methods in Entity Framework Core db context while using it inside a class inherited IHostedService
I have set up the example project inside the .NET Core repo for background tasks and I want to play with the real database records for something I need. 我已经在.NET Core存储库中设置了示例项目来执行后台任务,并且我想使用真实的数据库记录来满足我的需要。 But that seems impossible because the context methods only show those of Create Update Delete operations.
但这似乎是不可能的,因为上下文方法仅显示“创建更新删除”操作。 To be more specific, I cannot find a
Where
, FirstOrDefault
, First
, .... As in, I can't use the "Read" operations. 更具体地说,我找不到
Where
, FirstOrDefault
, First
,...。在这种情况下,我无法使用“读取”操作。 Has anyone encountered this? 有人遇到过这个吗? Am I doing something wrong?
难道我做错了什么?
The problem occurs on .NET Core version 2.2. .NET Core 2.2版上会出现此问题。 My
DbContext
inherits from Microsoft.EntityframeworkCore.DbContext
. 我的
DbContext
继承自Microsoft.EntityframeworkCore.DbContext
。
My DbContext
: 我的
DbContext
:
public class APIContext:DbContext
{
public static string ConnectionString { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySql(ConnectionString);
}
public DbSet<User> Users { get; set; }
public DbSet<Userinfo> Userinfos { get; set; }
public DbSet<Info> Infos { get; set; }
public DbSet<Picture> Pictures { get; set; }
public DbSet<Video> Videos { get; set; }
public DbSet<Feed> Feeds { get; set; }
public DbSet<Watchlist> Watchlists { get; set; }
public DbSet<Sale> Sales { get; set; }
public DbSet<CouponCode> CouponCodes { get; set; }
public DbSet<CouponUser> CouponUsers { get; set; }
public DbSet<Order> Orders { get; set; }
}
Hosted service: 托管服务:
internal class TimedHostedService : IHostedService, IDisposable
{
private readonly ILogger _logger;
private readonly APIContext context;
private Timer _timer;
public TimedHostedService(ILogger<TimedHostedService> logger, APIContext context)
{
_logger = logger;
this.context = context;
}
...
private void DoWork(object state) //this is called by the start method of the service within this class
{
context.Users. //this is where I don't get the other methods of DbContext
_logger.LogInformation("Timed Background Service is working.");
}
You should use IQueryable from System.Ling to work with those methods. 您应该使用System.Ling中的IQueryable来使用这些方法。 Here EntityFramework Core reference:
这里EntityFramework核心参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.