简体   繁体   English

在继承自IHostedService的类中使用Entity Framework Core db上下文时缺少方法

[英]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. 更具体地说,我找不到WhereFirstOrDefaultFirst ,...。在这种情况下,我无法使用“读取”操作。 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.");
    }

实例1

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.

相关问题 实体框架 - 如何不将继承的类添加到db - Entity Framework - how not to add inherited class to the db 实体框架核心 3 原始 SQL 缺少方法 - Entity Framework Core 3 raw SQL missing methods 实体框架核心在内存中存储db上下文之外的实体 - Entity Framework Core store an entity outside of db context in memory 在ObjectDataSource的静态方法中使用实体框架上下文 - Using an Entity Framework Context in static methods for an ObjectDataSource Entity Framework Core:单个数据库上下文实例的日志查询 - Entity Framework Core: Log queries for a single db context instance 使用实体框架 2.2 在 .net core 中启用 DB 上下文日志记录 - Enable DB context logging in .net core with entity framework 2.2 使用 DI 实现 Repository 和 Db Context .Net Core Console Entity Framework - Implement Repository and Db Context .Net Core Console Entity Framework with DI 实体框架核心 - 在可查询内部使用扩展方法 - Entity Framework Core - Use Extension Methods Inside Queryable .NET核心实体框架 - 在类库中为Context添加迁移 - .NET Core Entity Framework - Add migration for Context in class library 实体框架缺少ObjectSet方法 - Entity Framework missing ObjectSet methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM