ASP.NET MVC with a model in a separate assembly

Currently, I have a .NET solution that I am developing that includes several subprojects, including an ASP.NET MVC project. My model was divided into a separate assembly, because I need to use it from other other projects in the solution.

My model consists of an Entity Framework ADO.NET entity model. I decided to go with a singleton template for my model with the following code (SalsaEntities is 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;
        }
    }
}

Then I use SalsaEntities.Instance from my other assemblies. This works fine in the third project that I have, the Windows service, although I had to include the connection string in the App.Config project file.

, , SalsaEntities.Instance ASP.NET MVC. , Web.config , ...

<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>

... :

, EntityClient, .

, Windows , , .

,

P.S.: , , , Visual Studio , . , .

+3
2

, , .

, Singleton ( , MVC).

ObjectContext . . .

+3

ASP.NET, , ObjectContext ASP.NET. , :

, : http://www.ef-faq.org/objectcontext.html

, .

+1

Source: https://habr.com/ru/post/1722346/


All Articles