This app that I'm working on in .NET has one context and it's shared all across the app.
public partial class AppNameContext : DbContext
{
public AppNameContext()
{
}
public AppNameContext(DbContextOptions<AppNameContext> options)
: base(options)
{
Database.EnsureCreated();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// code
}
public virtual DbSet<Diagram> Diagram{ get; set; }
public virtual DbSet<Attribute> Attribute{ get; set; }
public virtual DbSet<Model3> Model3{ get; set; }
public virtual DbSet<Model4> Model4{ get; set; }
..
..
}
In my actual code, where I execute some logic, the app:
- Loads data from the database;
- Does some modification on it;
- Executes a stored procedure;
- Loads data from the database once again;
- Returns the data.
However, data returned in step 4 is the old data which I'm pretty sure it's cached because I found a way to check the cache and it was writing that Unchanged entites were cached even though some of them were updated. Judging by the research I've done so far, I need to create a new scoped context in order to retrieve the newest data or .Refresh() the context but I was getting an error that AppNameContext doesn't have Refresh implemented. For that purpose, I used
using(var _scope_context = new CognimartContext())
{
return await _scope_context.Diagram.Where(x => diagramIDs.Contains(x.ID)).Include(x => x.Attribute).ToListAsync();
}
where Diagram is
[Table("Diagram", Schema = "core")]
public partial class Diagram: BaseEntity
{
public Diagram()
{
Attribute = new HashSet<Attribute>();
}
public long ID{ get; set; }
[InverseProperty("Diagram")]
public virtual ICollection<Attribute> Attribute { get; set; }
}
and Attribute is
[Table("Attribute", Schema = "core")]
public partial class Attribute : BaseEntity
{
public long DiagramID { get; set; }
public long? ID{ get; set; }
public int ColumnOrder { get; set; }
}
When I execute this code with _scoped_context, I get the following error:
"No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext."
Do you know how can I put this scoped context in work or how can I avoid this caching?