C
C#6mo ago
Noriega

Exception about disposing DbContext

This exception I've been getting is so confusing since every other source I tried looking up for solutions doesn't exactly apply to my code. So, the exception I'm getting is that my db context gets disposed before I can use another operation on the db. Everywhere Ive checked for help had people screwing up asynchronous operations, which isnt the case for me. Here is my file for reference
- System.ObjectDisposedException: Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'ScathachDbContext'.
- System.ObjectDisposedException: Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'ScathachDbContext'.
Am using pooled db context btw
5 Replies
phaseshift
phaseshift6mo ago
Why aren't you using di for the context? Did you add a bad finalizer to your context?
IsNotNull
IsNotNull6mo ago
Does 'Guild' have any EF relation properties? You are putting the 'Guild' entry into a memory cache that outlives the DbContext it came from. If you try to access a relation property, it will try to use the disposed context to query the related entity and then throw an error like this.
Angius
Angius6mo ago
Assuming lazy loading is enabled
Noriega
Noriega6mo ago
my bad for late reply, but Guild isnt an EF object no however, GuildModel is this is actually how GuildModel is defined in another file
using Microsoft.Extensions.Configuration;
using Remora.Discord.API.Abstractions.Objects;

namespace Scathach.Database.Models;

[Table("Guild")]
public sealed class GuildModel(IConfigurationRoot configuration, IGuild guild)
{
[Key]
[Column(TypeName = "varchar(32)")]
public string Id => guild.ID.ToString();

[Column(TypeName = "varchar(100)")]
public string Name { get; set; } = guild.Name;

[Column(TypeName = "varchar(16)")]
public string Prefix { get; set; } = configuration["PREFIX"]!;
}
using Microsoft.Extensions.Configuration;
using Remora.Discord.API.Abstractions.Objects;

namespace Scathach.Database.Models;

[Table("Guild")]
public sealed class GuildModel(IConfigurationRoot configuration, IGuild guild)
{
[Key]
[Column(TypeName = "varchar(32)")]
public string Id => guild.ID.ToString();

[Column(TypeName = "varchar(100)")]
public string Name { get; set; } = guild.Name;

[Column(TypeName = "varchar(16)")]
public string Prefix { get; set; } = configuration["PREFIX"]!;
}
edit: optimized it a bit
jcotton42
jcotton426mo ago
Why do your models have constructors slap also not sure how EF could populate that ID prop, as it's get-only Making the model dependent on IConfiguration is also quite smelly