Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4071344
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:44:42+00:00 2026-05-20T16:44:42+00:00

I am injecting a new data context into each of my repositories with StructureMap.

  • 0

I am injecting a new data context into each of my repositories with StructureMap. The life cycle is per HTTP context. But I’m having some problems with “invalid cast specified errors” which I’ve narrowed down to “not disposing data contexts”. See my related question.

Where should I dispose of my data context (DB class) without modifying each read/write repository method? Wherever I modify data, I’ve used a new DB() in a using(...), so it’s only necessary for the IQueryable<xxx> Getxxx(...) methods, but some of them are interdependent, so I don’t want a dispose to cause DataContext disposed errors.

Bootstrapping:

public class DBServiceRegistry : Registry
{
    public DBServiceRegistry()
    {
        For<DB>().HttpContextScoped().Use(new DB());
        }
    }

Repository:

This data context is passed to my repositories who accept it like so:

public class SqlProductRepository : IProductRepository //, IDisposable
{
    private DB _db;

    public SqlProductRepository(DB db)
    {
        _db = db;
    }
    ...
}

Querying:

public IQueryable<Models.Product> GetProducts()
{
    var products =
        from p in _db.Products
        let images = GetProductImages().WithProductID(p.ProductId)
        select new Models.Product
        {
            ID = p.ProductId,
            ProductCode = p.ProductCode,
            Name = p.Name,
            Description = p.Description,
            Price = p.Price,
            DateModified = p.Date,
            ModelID = p.ModelId,
            Images = new LazyList<Models.ProductImage>(images),
            EditedBy = p.UserId,
            IsTaxable = p.IsTaxable,
            Inventory = p.InventoryStatus,
            Slug = p.Slug
        };

    return products;
}

I tried to make SqlProductRepository IDisposable, but the error persists. How can I cleanly dispose my data contexes (DB) without modifying each method (there are a lot)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:44:43+00:00Added an answer on May 20, 2026 at 4:44 pm

    Solution

    Implement IDisposable for repositories and use:

    For<DB>().HttpContextScoped().Use(() => new DB());
    

    Instead of

    For<DB>().HttpContextScoped().Use(new DB());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a online game. I got some problems with inserting new data
After inserting new html to DOM,I need to add some listeners to it. But
I'm having some problems posting JSON data to my rails controller and getting the
Does std::map move around already inserted values when inserting new data ?
So the main program is in C#. Inserting new records into a VFP database
I am new to mocking and dependency injection and need some guidance. My application
Sorry for the lengthy introduction but I think it best to explain the context
We're using Linq to SQL here. At present, our repositories tend to new up
I have some code that allows users to upload file attachments into a varbinary(max)
I have some data coming from other tiers and it represents an EF object.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.