Stale Navigation Property
I have a entity called ExamNotification with a navigation property called Exam. The first time a get an instance of ExamNotification. ExamNotification.Exam is correct. The next time get an instance of ExamNotification with the same Exam, the properties of the Exam are stale.
The EF context is called in a console application using the GenericHost.
The context is configured as follows:
services.AddDbContext<BoxContext>(cfg =>
{
cfg.UseSqlServer(configuration.GetConnectionString("BoxConnectionString"), sqloptions => sqloptions.CommandTimeout(120));
});
Context is injected into my consuming class through the built in DI.
Exams are included in the the query that retrieves ExamNotifications like this:
public IEnumerable<ExamNotification> GetCurrentExamNotifications() => _context.ExamNotifications
.Include(n => n.Exam)
.Where(n => n.SkippedByService == false && n.DateNotified == null)
.ToList();
@zanate4019 It looks like the code is probably re-using the same context instance for multiple unit-of-work. When using D.I., this usually happens if the context instance is registered as a singleton service, or registered as scoped (which the code above does) but then resolved from the default scope. If this is the case, then make sure a scope is being created for each use of the context instance.
If this doesn't seem to match what you are seeing, then please post a small, runnable project/solution or complete code listing that demonstrates the behavior you are seeing.
I have to admit I get a little confused with the different registration types if I am not in a ASP.NET application. Transient, Singleton, and Scoped make sense within the realm of http requests, but on a console app, I just registered all my services as Singleton.
public void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
var configuration = context.Configuration;
// configure Data
services.AddDbContext<BoxContext>(cfg =>
{
cfg.UseSqlServer(configuration.GetConnectionString("BoxConnectionString"), sqloptions => sqloptions.CommandTimeout(120));
});
// register repositories
services.AddSingleton<INotificationRepository, SignalRNotificationRepository>();
services.AddSingleton<IBoxRepository, EfCoreBoxRepository>();
// register engines
services.AddSingleton<INotificationEngine, DefaultNotificationEngine>();
services.AddSingleton<IBoxEngine, DefaultBoxEngine>();
// register managers
services.AddSingleton<INotificationManager, DefaultNotificationManager>();
// register hosted services
services.AddHostedService<ExternalNotificationService>();
}
What happens if I change my registrations to another type? I'd be ok with a new context on every call to the db. Although who would EF recognize changes without trying to persist the whole object graph? Is there way to register the context as transient?
@zanate4019 Transient should work. However, you'll probably need to manually dispose the context instance, since it's unlikely that the D.I. container will be able to do it for you.