locked
EntityFramework core - It fails to load all related entities RRS feed

  • Question

  • User433211055 posted

    Hi 

    I need to load all my related entity with updated data but when a call my method this does not happen.

    My application load al data only if I restart it.

    Why?

    This my code

     

    private readonly ScaDbContext scaDbContext;
    
     public WBadgeCompany(ScaDbContext scaDbContext)
            {
    
    this.scaDbContext = scaDbContext;
    
    }
    
    private List<AssignCompany> GetAssignCompanies()
            {
            
                List<AssignCompany> assignCompany = new List<AssignCompany>();
                assignCompany = scaDbContext.assignCompanies.Include(a => a.Badge)
                                                .Include(a => a.CompanyContact).ThenInclude(c => c.Company)
                                                .Include(a => a.UserStart)
                                                .Include(a => a.UserEnd).DefaultIfEmpty()
                                                .OrderBy(a => a.DataEnd)
                                                .ToList();
    
    
               // scaDbContext.Entry(assignCompany).Reload();
    
                return assignCompany;
            }

     BR

    Monday, October 19, 2020 10:35 AM

All replies

  • User1535942433 posted

    Hi cicciuzzo,

    Accroding to your description,could you tell us what's your dependency injection?And how your call the method which load the dbcontext data?

    I'm guessing that you have used a Singleton class which led to this problem.It will load the data and don't update.

    You could post more details of your codes to us.It will help us to solve your problems.

    Best regards,

    Yijing Sun

    Tuesday, October 20, 2020 8:54 AM
  • User-2054057000 posted

    Check the Startup.cs class to find out if you are registering the Database context file like this:

    public void ConfigureServices(IServiceCollection services)
            {
                services.AddDbContext<CompanyContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
     
                ...
            }

    Refer this tutorial to find out more on registering database context.

    If this is not the case then probably you are using Singleton patter which is causing this thing. You can post your full code of the Startup.cs class so that we can check.

    Wednesday, November 4, 2020 12:26 PM