views:

393

answers:

2

I currently have a .NET solution I'm developing that involves several sub-projects, including an ASP.NET MVC project. My model has been separated into a separate assembly because I need to use it from the various other projects in the solution.

My model consists of an ADO.NET Entity Framework entity model. I have decided to go with a singleton pattern for my model, with the following code (SalsaEntities being the name of my entity model):

partial class SalsaEntities
{
    private static SalsaEntities _instance = new SalsaEntities();

    /// <summary>
    /// Singleton instance of SalsaEntities.
    /// </summary>
    public static SalsaEntities Instance
    {
        get
        {
            return _instance;
        }
    }
}

I then consume SalsaEntities.Instance from my other assemblies. This works fine in a third project I have, a Windows Service, although I had to include the connection string in that project's App.Config file.

I am getting an exception, however, when I try to use the SalsaEntities.Instance from my ASP.NET MVC project. Despite including the following in the Web.config files in both the root of the project and in the Views directory...

<connectionStrings>
  <add name="SalsaEntities" connectionString="metadata=res://*/SalsaModel.csdl|res://*/SalsaModel.ssdl|res://*/SalsaModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=localhost;Initial Catalog=DbSalsa;Integrated Security=True;Pooling=False;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

...I still have the following exception thrown:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

The assemblies all have different namespaces, but since the Windows Service accesses it just fine, I don't think that's the problem.

Help?

Thanks, David

P.S.: One more quirk I've noticed is that when telling Visual Studio to create a new strongly-typed View, it generates a file not found error regarding the model assembly. I've seen this error discussed a few times on the web with no resolution.

+2  A: 

I'm not sure what is going on here, but Misha N asks a good question.

However I really think you should reconsider the Singleton pattern (at least for the MVC app).

Overtime your ObjectContext will contain more and more objects and as a result will slow down. See this tip for more on why.

Alex

Alex James
Since this is an ASP.NET application, shouldn't the life span of the singleton only exist until the page load completes?
David Pfeffer
A static variable will hang around until the app pool recycles.
Alex James
Interesting. Your comment still doesn't solve my problem, but I'll definitely have to change away from Singleton for performance reasons. Sadly, I still have the issue even if I locally instantiate SalsaEntities.
David Pfeffer
+1  A: 

Well, it started working seemingly spontaneously. However, the quirk I mentioned remains and I think it may be a bug.

David Pfeffer