开发者

ASP.NET MVC with Model in Separate Assembly

开发者 https://www.devze.com 2022-12-11 02:47 出处:网络
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 fro

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, 开发者_JAVA技巧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.


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


The singleton pattern is not good for ASP.NET based apps you will need to switch to something like a Repository pattern whereby you manage the ObjectContext at the ASP.NET Request level. There are several articles that discuss this:

You can start here if you like: http://www.ef-faq.org/objectcontext.html

Hope that helps..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号