Multiple Ajax Requests per MVC 4 View I'm using the repository pattern with a context and ninject as the IOC. I have a service which handles getting and setting page properties in the database.

public class MyContext : DbContext
{
    public MyContext() : base ("DefaultConnection")
    {
    }

    public DbSet&lt;PageProperty&gt; PageProperties { get; set; }
    public DbSet&lt;Contact&gt; Contacts { get; set; }
}

public class DefaultRepository : IRepository
{
    MyContext _context;
    public DefaultRepository(MyContext context)
    {
        _context = context;
    }

    public IQueryable&lt;PageProperty&gt; PageProperties { get { return _context.PageProperties; } }
    public IQueryable&lt;Contact&gt; Contacts { get { return _context.Contacts; } }
}

public class ModuleLoader : NinjectModule
{
    public ModuleLoader()
    {

    }

    public override void Load()
    {
        var context = new MyContext();
        context.Database.Initialize(false);
        Bind&lt;MyContext&gt;().ToConstant(context).InSingletonScope();
        Bind&lt;IRepository&gt;().To&lt;DefaultRepository&gt;();
        Bind&lt;IPagePropertyProvider&gt;().To&lt;DefaultPagePropertyProvider&gt;().InSingletonScope();
    }
}

public class DefaultPagePropertyProvider : IPagePropertyProvider
{
    IRepository _repository;
    object _syncLock = new object();

    public DefaultPagePropertyProvider (IRepository repository)
    {
        _repository = repository;
    }

    public string GetValue(string pageName, string propertyName
    {
        lock (_syncLock)
        {
            var prop = page.PageProperties.FirstOrDefault(x =&gt; x.Property.Equals(propertyName) &amp;&amp; x.PageName.Equals(pageName)).Value;

            return prop;
        }
     }
     public void SetValue(string pageName, string propertyName, string value)
     {
         var pageProp = _repository.PageProperties.FirstOrDefault(x =&gt; x.Property.Equals(propertyName) &amp;&amp; x.PageName.Equals(pageName));
         pageProp.Value = value;
         _repository.SaveSingleEntity(pageProp);
     }
}


In my view I am doing 3 ajax calls, one to get a list from contacts to fill out a table, one ajax call to determine how many pages i have depending on the page size I'm using, and one ajax call to set the page size that I want to use. so a select box changes the page size (How many contacts per page: [  30]) , a table that displays the contacts (generated from jquery which decifers json), and finally a div containing a list of page numbers to click. The workflow is, call GetContacts(), this function then queries the PageProperties to find out the page size to use, then call GetPages(), this function also queries PageProperties to find out what page size to use, SetPageSize() which sets the page size. So GetContacts() and GetPages() is used when a page is selected from the div, SetPageSize() then GetContacts() and GetPages() is called when the select box change event is fired. GetContacts() and GetPages() is only called when the first SetPageSize() $.ajax request is done() and there is a success from that function.

Now, before I added lock(syncLock) in the DefaultPageProperty service and before I added InSingletonScope to both that service and the context, I was getting two errors.


  The connection was not closed. The connection's current state is connecting.
  
  An EdmType cannot be mapped to CLR classes multiple times


I assumed because the connection was in a connecting state, that the context was being reused and reused and reused, so I thought putting that to SingletonScope() would mean that only one connection was made, then I thought the same about DefaultPageProperty and then because I was making async calls to that service, I should put a lock over the database querying.

It works, and the problems don't exist. But I don't know if what I have done is correct within the pattern I'm using, I'm wondering if I've missed something fundamental? My question is, is this a proper/viable solution which won't create any caveats later down the road? Have I actually solved the issue or just created more?
I don't think I completely know what is going on in your code.

but there is one thing that I think you are doing wrong.

you are creating a connection over the web and trying to force it to stay open.

your web application should 


Connect
Send or receive the information that it needs
Disconnect


you should never leave a connection open for extended periods of time.

every time you need something from the server, open a new connection, when you are finished close the connection.

I may be way out of line, but that struck me as off about the way you explained that part of your application