Forum

Cannot access a dis...
 
Notifications
Clear all

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 i...

2 Posts
1 Users
0 Reactions
1,424 Views
Posts: 24
Admin Registered
Topic starter
(@rijwan-ansari)
Illustrious Member
Joined: 5 years ago

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: 'ApplicationDBContext

1 Reply
Posts: 24
Admin Registered
Topic starter
(@rijwan-ansari)
Illustrious Member
Joined: 5 years ago

Solution:

Add ServiceLifetime.Transient in Dependency Injection of Application context as shown

 services.AddDbContext<ApplicationDBContext>(options =>
                options.UseSqlServer(configuration.GetConnectionString("ConnectionStr"),
                b => b.MigrationsAssembly(typeof(ApplicationDBContext).Assembly.FullName)), Se
Reply
Share: