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 7601241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:07:16+00:00 2026-05-30T23:07:16+00:00

I have an EF 4.2 EDMX model which I use in a multi-tenant application.

  • 0

I have an EF 4.2 EDMX model which I use in a multi-tenant application. I connect to about 100 databases that use the same EDM model. The first time each database is accessed, my working set goes up by ~12Mb, which seems mostly to be taken by the EDM metadata cache. The memory usage never goes back down. I would think the metadata/query cache could be shared since it is the same model.

Looking for suggestions to reduce my memory footprint, though I suspect I have no control over this.

NOTE: This same scenario is NOT a problem with CodeFirst (which we are using as well) but we have a lot of code that still uses the EDMX model and cannot convert it all right now.

Thanks!

  • 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-30T23:07:17+00:00Added an answer on May 30, 2026 at 11:07 pm

    I believe you can get what you want by caching the MetadataWorkspace yourself. This is essentially what DbContext does internally when using Code First. It’s not that easy to do, but I worked out a quick prototype that I think should work.

    The basic idea here is to let EF create a MetadataWorkspace once and then cache this and use it explicitly each time you need to create a context instance. This is obviously only valid if each context instance is using the same model–i.e. the same EDMX. To make this work I created a derived ObjectContext that handles the caching:

    public class SingleModelCachingObjectContext : ObjectContext
    {
        private static readonly object WorkspaceLock = new object();
        private static MetadataWorkspace _workspace;
    
        public SingleModelCachingObjectContext(string connectionStringName)
            : base(CreateEntityConnection(connectionStringName))
        {
        }
    
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                ((EntityConnection)Connection).StoreConnection.Dispose();
            }
        }
    
        private static EntityConnection CreateEntityConnection(string connectionStringName)
        {
            lock (WorkspaceLock)
            {
                if (_workspace == null)
                {
                    _workspace = new EntityConnection("name=" + connectionStringName).GetMetadataWorkspace();
                }
            }
    
            var builder =
                new DbConnectionStringBuilder
                {
                    ConnectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString
                };
    
            var storeConnection = DbProviderFactories.GetFactory((string)builder["provider"]).CreateConnection();
            storeConnection.ConnectionString = (string)builder["provider connection string"];
    
            return new EntityConnection(_workspace, storeConnection);
        }
    }
    

    You would then use this by creating a constructor on your DbContext class like so:

    public MyDbContext(string connectionStringName)
        : base(new SingleModelCachingObjectContext(connectionStringName),
               dbContextOwnsObjectContext: true)
    {
    }
    

    This is how it works. When you create an instance of your DbContext it in turn creates an instance of SingleModelCachingObjectContext passing in the name of the EF connection string that you want to use. It also tells DbContext to dispose of this ObjectContext when the DbContext is disposed.

    In SingleModelCachingObjectContext the EF connection string is used to create a MetadataWorkspace and caches it in a static field once created. This is very simple caching and simple thread safety with a lock–feel free to make it more suited to your app’s needs.

    With the MetadataWorkspace obtained, the EF connection string is now parsed to obtain the store connection string and provider. This is then used to create a normal store connection.

    The store connection and cached MetadataWorkspace are used to create an EntityConnection and then an ObjectContext that will use the cached MetadataWorkspace instead of using the normal caching mechanisms.

    This ObjectContext is used to back the DbContext. The Dispose Method is overridden so that the store connection does not leak. When the DbContext is disposed it will dispose the ObjectContext, which will in turn call Dispose and the store connection will be disposed.

    I haven’t really tested this other than to make sure it runs. It would be very interesting to know whether or not it really helps your memory usage issues.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an edmx model which I have draged 2 tables onto - One
i have an entity-model file (edmx) file which contains few tables and few stored
I have a client application (WPF, C#, .net4) which uses POCO entity model connected
I have a simple ADO.NET Entity Framework 4.0 model (edmx) which defines database tables
I have an MVVM application that uses a listbox which is populated with images.
I have in my web application an ADO.NET Entity-Framework *.edmx file. When I browse
I have a project in which I have a database model class provided along
We have several SQL scripts which are generated from an Entity Model. They need
I have an entity model build using EF4.1 code first, which uses a WCF
I have a Model1.edmx, which has several tables. Now, I do not want all

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.